2016-12-26 142 views
0

我做了一个双列列表视图根据this教程,其工作正常,但除了点击行我还需要找到哪列被点击(列1或2),是否有可能为了达成这个?查找项目点击多列ListView

这是我的主要活动:

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> 
+0

发表您的R.layout.activity_main – Noorul

+0

我刚刚发布了他们..感谢 – Chico3001

回答

1

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

+0

不知道我能做到这一点...我会找一个很好的教程关于如何制作网格适配器...:P – Chico3001

+0

你可以看看这个教程.. http://www.stealthcopter.com/blog/2010/09/android-creating-a-custom-adapter-换的GridView-buttonadapter / –

0

根据您提供的链接,它只是自定义的listview与多个textview的。

这方面没有任何复杂性。只需将该标签设置为该文本视图。

一样,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(); 


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

     } 

    });