【问题标题】:Find item clicked on multicolumn ListView查找在多列 ListView 上单击的项目
【发布时间】:2016-12-26 07:00:11
【问题描述】:

我正在根据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>

【问题讨论】:

  • 发布你的 R.layout.activity_main
  • 我刚刚发布了它们.. 谢谢

标签: android listview baseadapter onitemclicklistener


【解决方案1】:

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

【讨论】:

  • 不知道我能做到这一点......我会寻找一个关于如何制作网格适配器的好教程......:P
  • 你可以看看这个教程..stealthcopter.com/blog/2010/09/…
【解决方案2】:

根据您提供的链接,它只是具有多个文本视图的自定义列表视图。

这并不复杂。只需将标签设置为该文本视图。

如 tv0.setTag("0"),tv1.setTag("1),tv2.setTag("2"),tv3.setTag("3"). 并将 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();


  }
 });

【讨论】:

    【解决方案3】:

    是的,可以通过适配器来实现,这是我的一个例子

    在适配器的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;
    }
    

    【讨论】:

      【解决方案4】:

      在你的代码中修改它

        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
      
              }
      
          });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-08-22
        • 1970-01-01
        • 2011-03-24
        相关资源
        最近更新 更多