查找在多列 ListView 上单击的项目
Find item clicked on multicolumn ListView
我正在根据 this 教程制作一个两列列表视图,它工作正常但除了单击的行之外我还需要找到单击的列(第 1 列或第 2 列),是否可能要做到这一点?
这是我的主要内容 Activity:
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>
Activity 主要 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>
改用 GridView
,然后您就可以轻松地使用 gridView.setOnItemClickListener()
方法。干杯!
根据您提供的link,它只是带有多个文本视图的自定义列表视图。
这并不复杂。只需将标签设置为该文本视图即可。
喜欢,tv0.setTag(“0”),tv1.setTag(“1”),tv2.setTag(“2”),tv3.setTag(“3”)。并为所有文本视图初始化 onClikListener。
当用户点击时,您将获得被点击的视图。喜欢
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();
}
});
是的,可以通过适配器来完成这里是我的一个例子
在adapter的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;
}
在你的代码中修改这个
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
}
});
我正在根据 this 教程制作一个两列列表视图,它工作正常但除了单击的行之外我还需要找到单击的列(第 1 列或第 2 列),是否可能要做到这一点?
这是我的主要内容 Activity:
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>
Activity 主要 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>
改用 GridView
,然后您就可以轻松地使用 gridView.setOnItemClickListener()
方法。干杯!
根据您提供的link,它只是带有多个文本视图的自定义列表视图。
这并不复杂。只需将标签设置为该文本视图即可。
喜欢,tv0.setTag(“0”),tv1.setTag(“1”),tv2.setTag(“2”),tv3.setTag(“3”)。并为所有文本视图初始化 onClikListener。
当用户点击时,您将获得被点击的视图。喜欢
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();
}
});
是的,可以通过适配器来完成这里是我的一个例子
在adapter的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;
}
在你的代码中修改这个
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
}
});