-1

如何制作每个项目可点击的自定义列表视图(我的意思是,我正在制作一个包含一个图像按钮、两个文本视图的列表视图,我想在用户单击项目时访问位置,然后它不显示可点击。)。

4

4 回答 4

1

代码:

@Override
public void onListItemClick(ListView l, View v, int position, long id) {

       // here position variable holding the ListView position which clicked by user 
}
于 2013-03-14T06:52:16.067 回答
0

我想这会对你有所帮助..

lv.setOnItemClickListener(new AdapterView.OnItemClickListener() 
 { 
      @Override 
      public void onItemClick(AdapterView arg0, View arg1,int position, long arg3) 
      { 

      } 
 });
于 2013-03-14T06:43:55.607 回答
0

You can achieve this in the getView method of the Adapter you have applied to your customListView.

It would roughly be like using View v=customView.findViewById(id of your view i.e image etc.) also you have to set onClickListener on the particular view element i.e v in my case.

-- Edit --

A similar question Go through this linked question and the accepted answer also.

于 2013-03-14T06:46:52.877 回答
0

您可以在包含Button, 和 2的列表视图中扩展自定义布局,TextView如下所示:

listview_row.xml

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
          android:orientation="horizontal" >

<TextView
    android:id="@+id/textView2"
    android:layout_width="75dp"
    android:layout_height="75dp"
    android:scaleType="fitXY"
     />

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="0dp"
    android:layout_marginTop="15dp"
    android:text="Medium Text"
    android:textSize="15dp" />

  <ImageButton
    android:id="@+id/imageview1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_launcher"
      />

   </LinearLayout>

在您的主要活动布局中添加列表视图,如下所示:

主要的.xml

     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical" >
  <ListView
            android:id="@+id/listView1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
 </ListView>
</LinearLayout>

如上所述创建布局后,创建一个适配器类以在您的列表视图中添加自定义布局,并扩展BaseAdapter.

 public class App_Adapter extends BaseAdapter implements OnClickListener{

在那个 adpater 类的getView方法中,您需要扩展您的布局。

 @Override
  public View getView(int position, View convertView, ViewGroup parent) {
         // TODO Auto-generated method stub
         View mView=convertView;
            if (convertView == null)  
              mView = inflater.inflate(R.layout.listview_row, null);

           TextView text1=(TextView)mView.findViewById(R.id.textView1));
           TextView text2=(TextView)mView.findViewById(R.id.textView2));
           ImageButton imagbtn=(ImageButton)mView.findViewById(R.id.imageview1);
         //set the image button click listener
      imagbtn..setOnClickListener(this);
   return mView;
   }

之后,在您的活动中,您可以访问列表视图并为其设置 onItemclick 侦听器以访问列表视图的列表项,如下所示:

  public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main); 
      ListView lv = (ListView)findViewById(R.id.listView1);
    lv.setAdapter(new App_Adapter());
    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() 
     { 
       @Override 
       public void onItemClick(AdapterView arg0, View arg1,int position, long arg3) 
      { 
        } 
      });
     }
于 2013-03-14T09:34:11.440 回答