android 中的 ListView 未突出显示我正在选择的行
ListView in android is not highlighting the row i am selecting
当我在 ListView 中点击一行时,ListView 没有显示任何效果我想显示一些效果或任何颜色等,就像在 iOS 中,当我们 select 或点击我正在为 ListView 使用自定义视图和自定义适配器并引入自定义视图中的一行
我在这里创建一个带有自定义视图的自定义适配器
CustomContactsHomeAdapter customContactsAdapter = new CustomContactsHomeAdapter(HomeView.this, R.layout.custom_contact_cell, userBean_home_Search.getData(),
listView.setAdapter(customContactsAdapter);
这是我的自定义适配器
public class CustomContactsHomeAdapter extends ArrayAdapter<UserBean_Home.DataBean> {
public UserBean_Home userBean;
Context mContext;
public CustomContactsHomeAdapter(Context context, int textViewResourceId) {
super(context, textViewResourceId);
}
public CustomContactsHomeAdapter(Context context, int resource, List<UserBean_Home.DataBean> need, UserBean_Home abc) {
super(context, resource , need);
this.mContext = context;
this.userBean = abc;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
if(convertView==null) {
LayoutInflater vi;
vi = LayoutInflater.from(mContext);
convertView = vi.inflate(R.layout.custom_contact_cell, null);
TextView contactName = (TextView) convertView.findViewById(R.id.contactName);
TextView contactDesc = (TextView) convertView.findViewById(R.id.contactAddress);
ImageView profileImage = (ImageView) convertView.findViewById(R.id.profileImage);
UserBean_Home.DataBean dataBean = userBean.getData().get(position);
if(dataBean.getR_type().equalsIgnoreCase("1")) {
contactName.setText(dataBean.getFull_name());
contactDesc.setText("@"+dataBean.getUser_name());
Picasso.with(mContext)
.load(GlobalBean.IMAGES_URL+dataBean.getImage())
.placeholder(R.drawable.noimage)
.into(profileImage);
} else {
contactName.setText(dataBean.getGroup_title());
contactDesc.setText("@"+dataBean.getGroup_description());
Picasso.with(mContext)
.load(GlobalBean.IMAGES_URL+dataBean.getGroup_cover_image())
.placeholder(R.drawable.noimage)
.into(profileImage);
}
// if(dataBean.getUnread_messages().equalsIgnoreCase("0") == false) {
// TextView undreadMessages = (TextView) convertView.findViewById(R.id.undreadMessages);
// undreadMessages.setVisibility(View.VISIBLE);
// undreadMessages.setText(dataBean.getUnread_messages());
// }
}
return convertView;
}
@Override
public int getViewTypeCount() {
return getCount();
}
@Override
public int getItemViewType(int position) {
return
}
这是 XML
中的 ListView
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/listView"
android:layout_below="@id/L1 />
有人吗??
只需使用
listview.setOnItemClickListener
像下面这样设置适配器后
listview.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,int position, long arg3)
{
Toast.makeText(yourActivity.this, "" + position, Toast.LENGTH_SHORT).show();
arg1.setBackgroundColor(Color.GREEN);
}
});
这里您需要的是一个应用于 ListView
项的选择器。这是一个例子:
选择器mySelector.xml:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/pressed" android:state_pressed="true"/>
<item android:drawable="@drawable/unpressed" />
</selector>
项目示例:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/ftaccountrow"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/mySelector" >
.....
</RelativeLayout>
当我在 ListView 中点击一行时,ListView 没有显示任何效果我想显示一些效果或任何颜色等,就像在 iOS 中,当我们 select 或点击我正在为 ListView 使用自定义视图和自定义适配器并引入自定义视图中的一行
我在这里创建一个带有自定义视图的自定义适配器
CustomContactsHomeAdapter customContactsAdapter = new CustomContactsHomeAdapter(HomeView.this, R.layout.custom_contact_cell, userBean_home_Search.getData(),
listView.setAdapter(customContactsAdapter);
这是我的自定义适配器
public class CustomContactsHomeAdapter extends ArrayAdapter<UserBean_Home.DataBean> {
public UserBean_Home userBean;
Context mContext;
public CustomContactsHomeAdapter(Context context, int textViewResourceId) {
super(context, textViewResourceId);
}
public CustomContactsHomeAdapter(Context context, int resource, List<UserBean_Home.DataBean> need, UserBean_Home abc) {
super(context, resource , need);
this.mContext = context;
this.userBean = abc;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
if(convertView==null) {
LayoutInflater vi;
vi = LayoutInflater.from(mContext);
convertView = vi.inflate(R.layout.custom_contact_cell, null);
TextView contactName = (TextView) convertView.findViewById(R.id.contactName);
TextView contactDesc = (TextView) convertView.findViewById(R.id.contactAddress);
ImageView profileImage = (ImageView) convertView.findViewById(R.id.profileImage);
UserBean_Home.DataBean dataBean = userBean.getData().get(position);
if(dataBean.getR_type().equalsIgnoreCase("1")) {
contactName.setText(dataBean.getFull_name());
contactDesc.setText("@"+dataBean.getUser_name());
Picasso.with(mContext)
.load(GlobalBean.IMAGES_URL+dataBean.getImage())
.placeholder(R.drawable.noimage)
.into(profileImage);
} else {
contactName.setText(dataBean.getGroup_title());
contactDesc.setText("@"+dataBean.getGroup_description());
Picasso.with(mContext)
.load(GlobalBean.IMAGES_URL+dataBean.getGroup_cover_image())
.placeholder(R.drawable.noimage)
.into(profileImage);
}
// if(dataBean.getUnread_messages().equalsIgnoreCase("0") == false) {
// TextView undreadMessages = (TextView) convertView.findViewById(R.id.undreadMessages);
// undreadMessages.setVisibility(View.VISIBLE);
// undreadMessages.setText(dataBean.getUnread_messages());
// }
}
return convertView;
}
@Override
public int getViewTypeCount() {
return getCount();
}
@Override
public int getItemViewType(int position) {
return
}
这是 XML
中的 ListView<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/listView"
android:layout_below="@id/L1 />
有人吗??
只需使用
listview.setOnItemClickListener
像下面这样设置适配器后
listview.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,int position, long arg3)
{
Toast.makeText(yourActivity.this, "" + position, Toast.LENGTH_SHORT).show();
arg1.setBackgroundColor(Color.GREEN);
}
});
这里您需要的是一个应用于 ListView
项的选择器。这是一个例子:
选择器mySelector.xml:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/pressed" android:state_pressed="true"/>
<item android:drawable="@drawable/unpressed" />
</selector>
项目示例:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/ftaccountrow"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/mySelector" >
.....
</RelativeLayout>