onclick Listener for custom listview

时间:2014-12-01 19:39:09

标签: java android android-layout listview android-listview

我正在开发一个带有自定义列表视图的Android应用程序(带有单个图像和两个文本视图)

班主要活动有......

public class MainActivity extends ActionBarActivity {
String[] memetitles;
String[] memedesc;
ListView list;
int[] images = {R.drawable.angry_icon,R.drawable.happy_icon,R.drawable.kid_icon,R.drawable.scare_icon,R.drawable.warn_icon};
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Resources res = getResources();
    memetitles = res.getStringArray(R.array.titles);
    memedesc = res.getStringArray(R.array.notneed);
    list = (ListView) findViewById(R.id.listView1);
    Itarray adapter = new Itarray(this,memetitles,memedesc,images);
    list.setAdapter(adapter);
    setTitle("Tamil Memer");
}

将两个布局组合到一个列表视图中的类有

class Itarray extends ArrayAdapter<String>{
Context context;
int[] image;
String[] tit1;
String[] tit2;
Itarray(Context c , String[] titles ,String[] notneed, int imgs[])
{
    super(c , R.layout.single_row,titles);
    this.context = c;
    this.image = imgs;
    this.tit1 = titles;
    this.tit2 = notneed;
}

    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        View row = convertView;
        if(row == null)
        {
        LayoutInflater inf = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        row = inf.inflate(R.layout.single_row , parent , false);
        }
        ImageView myImage = (ImageView) row.findViewById(R.id.imageView1);
        TextView tit = (TextView) row.findViewById(R.id.textView1);
        TextView des = (TextView) row.findViewById(R.id.textView2);
        myImage.setImageResource(image[position]);
        tit.setText(tit1[position]);
        des.setText(tit2[position]);
        return row;
    }   

}

两个布局文件: (带有图像和文本视图的那个)

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="top"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.itassistors.tmg.MainActivity" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:layout_toEndOf="@+id/imageView1"
    android:layout_toRightOf="@+id/imageView1"
    android:text="@string/simple"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/imageView1"
    android:layout_alignLeft="@+id/textView1"
    android:layout_alignStart="@+id/textView1"
    android:text="@string/simple"
    android:textAppearance="?android:attr/textAppearanceSmall" />

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:layout_alignParentLeft="true"
    android:layout_alignTop="@+id/textView1"
    android:src="@drawable/happy_icon" />

(带有listview的那个)

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.itassistors.tmg.MainActivity" >

<ListView
    android:id="@+id/listView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true" >
</ListView>

我想知道如何以及在何处为此自定义列表视图放置onClickListener() 提前致谢

2 个答案:

答案 0 :(得分:0)

您有两种选择:

1)活动中设置点击监听器:

list.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
            // Do your stuff here
        }

 });

2)适配器中设置点击监听器:

public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        View row = convertView;
        if(row == null)
        {
        LayoutInflater inf = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        row = inf.inflate(R.layout.single_row , parent , false);
        }
        ImageView myImage = (ImageView) row.findViewById(R.id.imageView1);
        TextView tit = (TextView) row.findViewById(R.id.textView1);
        TextView des = (TextView) row.findViewById(R.id.textView2);
        myImage.setImageResource(image[position]);
        tit.setText(tit1[position]);
        des.setText(tit2[position]);
        row.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                  // Do your stuff here
            }
        });
        return row;
    }

答案 1 :(得分:0)

如果你在适配器中这样做,你将遵循以下代码:

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);


    ViewHolderItem holder;

    if (convertView == null) {
        convertView = inflater.inflate(R.layout.your_list_item, null);
        View row = convertView;

        holder = new ViewHolderItem();
        convertView.setTag(holder);
        row.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.v(LOG_TAG, "ROW PRESSED");
                context.startActivity(new Intent(context, YourAction.class));
            }
        });

    } else {
        holder = (ViewHolderItem) convertView.getTag();
    }
    return convertView;

}