Last active
December 31, 2015 18:49
-
-
Save Dzakus/8029064 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | |
package="pl.com.android.listview.multi.item" | |
android:versionCode="1" | |
android:versionName="1.0" > | |
<uses-sdk | |
android:minSdkVersion="8" | |
android:targetSdkVersion="18" /> | |
<application | |
android:allowBackup="true" | |
android:icon="@drawable/ic_launcher" | |
android:label="@string/app_name"> | |
<activity | |
android:name="pl.com.android.listview.multi.item.MainActivity" | |
android:label="@string/app_name" > | |
<intent-filter> | |
<action android:name="android.intent.action.MAIN" /> | |
<category android:name="android.intent.category.LAUNCHER" /> | |
</intent-filter> | |
</activity> | |
</application> | |
</manifest> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package pl.com.android.listview.multi.item; | |
import pl.com.android.listview.multi.item.MainActivity.RowItem; | |
import android.content.Context; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.View.OnClickListener; | |
import android.view.ViewGroup; | |
import android.widget.ArrayAdapter; | |
import android.widget.TextView; | |
public class CustomAdapter extends ArrayAdapter<RowItem> implements OnClickListener { | |
private static final int TAG_POSITION = R.id.tag_position; | |
public interface onClickListener { | |
void onClickView(int position, View v); | |
} | |
private LayoutInflater mInflater; | |
private onClickListener mListener; | |
public CustomAdapter(Context context, RowItem[] objects) { | |
super(context, R.layout.row_item, objects); | |
mInflater = LayoutInflater.from(context); | |
} | |
public void setListenerView(onClickListener l) { | |
mListener = l; | |
} | |
public static class ViewHolder { | |
TextView text1; | |
TextView text2; | |
TextView text3; | |
} | |
@Override | |
public View getView(int position, View convertView, ViewGroup parent) { | |
ViewHolder holder; | |
if (convertView == null) { | |
convertView = mInflater.inflate(R.layout.row_item, parent, false); | |
holder = new ViewHolder(); | |
holder.text1 = (TextView) convertView.findViewById(R.id.textView1); | |
holder.text2 = (TextView) convertView.findViewById(R.id.textView2); | |
holder.text3 = (TextView) convertView.findViewById(R.id.textView3); | |
holder.text1.setOnClickListener(this); | |
holder.text2.setOnClickListener(this); | |
holder.text3.setOnClickListener(this); | |
convertView.setTag(holder); | |
} else { | |
holder = (ViewHolder) convertView.getTag(); | |
} | |
RowItem item = getItem(position); | |
holder.text1.setText(item.title); | |
holder.text1.setTag(TAG_POSITION, position); | |
holder.text2.setText(item.ocena); | |
holder.text2.setTag(TAG_POSITION, position); | |
holder.text3.setText(item.desc); | |
holder.text3.setTag(TAG_POSITION, position); | |
return convertView; | |
} | |
@Override | |
public void onClick(View v) { | |
int position = (Integer) v.getTag(TAG_POSITION); | |
if (mListener != null) { | |
mListener.onClickView(position, v); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package pl.com.android.listview.multi.item; | |
import java.util.ArrayList; | |
import pl.com.android.listview.multi.item.CustomAdapter.onClickListener; | |
import android.app.AlertDialog; | |
import android.app.ListActivity; | |
import android.os.Bundle; | |
import android.view.View; | |
public class MainActivity extends ListActivity implements onClickListener { | |
private RowItem[] mData; | |
private CustomAdapter mAdapter; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
ArrayList<RowItem> dataList = new ArrayList<RowItem>(); | |
for (int i = 0; i < 10000; i++) { | |
dataList.add(new RowItem("title " + i, "ocena " + i, "desc " + i)); | |
} | |
mData = dataList.toArray(new RowItem[] {}); | |
mAdapter = new CustomAdapter(this, mData); | |
setListAdapter(mAdapter); | |
mAdapter.setListenerView(this); | |
} | |
@Override | |
public void onClickView(int position, View v) { | |
new AlertDialog.Builder(this) | |
.setMessage(String.format("view ID: 0x%08X \nposition: %d", v.getId(), position)) | |
.setPositiveButton(android.R.string.ok, null) | |
.show(); | |
} | |
public static class RowItem { | |
String title; | |
String ocena; | |
String desc; | |
public RowItem(String title, String url, String desc) { | |
this.title = title; | |
this.ocena = url; | |
this.desc = desc; | |
} | |
@Override | |
public String toString() { | |
return title; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" > | |
<Button | |
android:id="@+id/textView1" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_alignParentLeft="true" | |
android:layout_alignParentTop="true" | |
android:textAppearance="?android:attr/textAppearanceLarge" /> | |
<Button | |
android:id="@+id/textView3" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_alignParentRight="true" | |
android:layout_alignParentTop="true" | |
android:textAppearance="?android:attr/textAppearanceLarge" /> | |
<Button | |
android:id="@+id/textView2" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_alignParentTop="true" | |
android:layout_centerHorizontal="true" | |
android:textAppearance="?android:attr/textAppearanceLarge" /> | |
</RelativeLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<resources> | |
<string name="app_name">ListViewMultiItem</string> | |
</resources> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment