查找单击多列ListView的项目

时间:2016-12-26 07:00:11

标签: android listview baseadapter onitemclicklistener

我正在根据this教程制作一个两列列表视图,它工作正常但除了点击的行之外我还需要找到点击了哪一列(第1列或第2列),是否有可能实现此?

这是我的主要活动:

public class MainActivity extends AppCompatActivity {

    private ArrayList<HashMap<String, String>> list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        final ListView listView=(ListView)findViewById(R.id.hotelsList);

        populateList();

        ListViewAdapter adapter=new ListViewAdapter(this, list);
        listView.setAdapter(adapter);


        listView.setOnItemClickListener(new AdapterView.OnItemClickListener()
        {
            @Override
            public void onItemClick(AdapterView<?> parent, final View view, int position, long id)
            {
                // TODO find which column was clicked
            }

        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    public void populateList(){
        list=new ArrayList<HashMap<String,String>>();

        HashMap<String,String> temp=new HashMap<String, String>();
        temp.put("Hotel_Name_1", "Hotel 1");
        temp.put("Hotel_Description_1", "Descripcion Hotel 1");
        temp.put("Hotel_Name_2", "Hotel 2");
        temp.put("Hotel_Description_2", "Descripcion Hotel 2");
        list.add(temp);

        HashMap<String,String> temp2=new HashMap<String, String>();
        temp2.put("Hotel_Name_1", "Hotel 3");
        temp2.put("Hotel_Description_1", "Descripcion Hotel 3");
        temp2.put("Hotel_Name_2", "Hotel 4");
        temp2.put("Hotel_Description_2", "Descripcion Hotel 4");
        list.add(temp2);

        HashMap<String,String> temp3=new HashMap<String, String>();
        temp3.put("Hotel_Name_1", "Hotel 5");
        temp3.put("Hotel_Description_1", "Descripcion Hotel 5");

        temp3.put("Hotel_Name_2", "Hotel 6");
        temp3.put("Hotel_Description_2", "Descripcion Hotel 6");
        list.add(temp3);
    }
}

列出项目XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:layout_marginLeft="@dimen/image_margin_small"
        android:layout_marginRight="@dimen/image_margin_small_half"
        android:layout_marginTop="@dimen/image_margin_small"
        android:layout_marginBottom="@dimen/image_margin_small"
        android:id="@+id/item1">

        <TextView
            android:id="@+id/hotelName1"
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:layout_weight="1"
            android:text="Hotel 1"
            android:textAppearance="@android:style/TextAppearance.Large" />

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:srcCompat="@drawable/hotel"
            android:id="@+id/hotelImage1"
            android:scaleType="fitCenter"
            android:adjustViewBounds="true" />

        <TextView
            android:text="TextView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/hotelDescription1"
            android:layout_weight="1" />

    </LinearLayout>

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:layout_marginLeft="@dimen/image_margin_small_half"
        android:layout_marginRight="@dimen/image_margin_small"
        android:layout_marginTop="@dimen/image_margin_small"
        android:layout_marginBottom="@dimen/image_margin_small"
        android:id="@+id/item2">

        <TextView
            android:id="@+id/hotelName2"
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:layout_weight="1"
            android:text="Hotel 2"
            android:textAppearance="@android:style/TextAppearance.Large" />

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:srcCompat="@drawable/hotel"
            android:id="@+id/hotelImage2"
            android:scaleType="fitCenter"
            android:adjustViewBounds="true" />

        <TextView
            android:text="TextView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/hotelDescription2"
            android:layout_weight="1" />
    </LinearLayout>

</LinearLayout>

活动主XML:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="com.miempresaenlanube.hoteles360.MainActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

    </android.support.design.widget.AppBarLayout>

    <include layout="@layout/content_main" />

</android.support.design.widget.CoordinatorLayout>

content_main XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/content_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.miempresaenlanube.hoteles360.MainActivity"
    tools:showIn="@layout/activity_main">

    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/hotelsList" />

</RelativeLayout>

4 个答案:

答案 0 :(得分:1)

使用GridView代替,然后您可以轻松使用gridView.setOnItemClickListener()方法。干杯!

答案 1 :(得分:0)

根据您提供的链接,它只是自定义列表视图,包含多个textview&#39; s。

没有复杂性。只需将标记设置为该textview。

喜欢,tv0.setTag(&#34; 0&#34;),tv1.setTag(&#34; 1),tv2.setTag(&#34; 2&#34;),tv3.setTag(&# 34; 3&#34)。并将onClikListener初始化为所有textview。

当用户点击时,您将获得点击的视图。喜欢

 tv0.setOnClickListener(new View.OnClickListener() {
  @Override
  public void onClick(View v) {
    TextView tv=(TextView)v;
    String pos=(String)tv.getTag();

    //you can do like below also.
    //String pos=(String)v.getTag();


  }
 });

答案 2 :(得分:0)

是的,可以从适配器这里做到这一点是我的一个例子

在适配器的getview方法中,在下面设置onClickListner

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    convertView = null;

    ViewHolderCategory holder;
    holder = new ViewHolderCategory();

    if (convertView == null) {

        convertView = mInflater.inflate(R.layout.item_category_list, null);

        holder.tv = (TextView) convertView.findViewById(R.id.textView1);
        holder.tv.setText(mTotalCategoriesItems.get(position)
                .getCategoryTitle());

        convertView.setTag(holder);
    }

 holder.tv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
    //Do Something Here
}
 });

 //Similary keep on click listner for other views inside listView here
    return convertView;
}

答案 3 :(得分:0)

在您的代码中修改此内容

  listView.setOnItemClickListener(new AdapterView.OnItemClickListener()
    {
        @Override
        public void onItemClick(AdapterView<?> parent, final View view, int position, long id)
        {
            HashMap<String,String> selectedItem=list.get(position);//you will get the clicked item here you can use further as you want

        }

    });