当长按突出显示自定义列表视图项 [英] Highlight custom listview item when long click
问题描述
我创建了一个自定义的列表视图
其中有一个的ImageView
在左边,一个的TextView
在右侧。并实施多选列表视图
与动作条
上下文菜单。
现在的问题是,当我长点击它并没有得到突出显示的项目。
下面是我如何设置列表视图
在我的 ListFragment
PasswordsFragment.java
包mohd.itcs.safewallet;
进口android.os.Bundle;
进口android.support.v4.app.ListFragment;
进口android.view.ActionMode;
进口android.view.Menu;
进口android.view.MenuItem;
进口android.view.View;
进口android.widget.AbsListView.MultiChoiceModeListener;
进口android.widget.AdapterView;
进口android.widget.AdapterView.OnItemLongClickListener;
进口android.widget.ListView;
公共类PasswordsFragment扩展ListFragment {
私人字符串标题[] = {ITEM1,项目2,项目3};
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
setListAdapter(新CustomPasswordsList(getActivity(),职称));
}
@覆盖
公共无效onActivityCreated(包savedInstanceState){
super.onActivityCreated(savedInstanceState);
/ *
*设置多重选择模式
* /
。getListView()setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
getListView()。setOnItemLongClickListener(新OnItemLongClickListener(){
@覆盖
公共布尔onItemLongClick(适配器视图<>列表视图,查看项目,
INT位置,长的id){
getListView()setItemChecked(位置,真正的)。
返回true;
}
});
getListView()。setMultiChoiceModeListener(新MultiChoiceModeListener(){
@覆盖
公共布尔onActionItemClicked(ActionMode为arg0,菜单项ARG1){
// TODO自动生成方法存根
返回false;
}
/ *
*充气动作条菜单的密码多重选择
* /
@覆盖
公共布尔onCreateActionMode(ActionMode为arg0,菜单菜单){
getActivity()。getMenuInflater()。膨胀(
R.menu.passwords_context_menu,菜单);
返回true;
}
@覆盖
公共无效onDestroyActionMode(ActionMode为arg0){
}
@覆盖
在prepareActionMode公共布尔(ActionMode为arg0,菜单ARG1){
返回false;
}
@覆盖
公共无效onItemCheckedStateChanged(ActionMode actionMode,
INT位置,长的ID,布尔ARG3){
/ *
*更改标题栏来选择号码
* /
INT checkedItems = getListView()getCheckedItemCount()。
actionMode.setTitle(将String.valueOf(checkedItems)+选择);
}
});
}
}
CustomPasswordsList.java
包mohd.itcs.safewallet;
进口android.content.Context;
进口android.view.LayoutInflater;
进口android.view.View;
进口android.view.ViewGroup;
进口android.widget.ArrayAdapter;
进口android.widget.ImageView;
进口android.widget.TextView;
公共类CustomPasswordsList扩展ArrayAdapter<字符串> {
私人最终上下文的背景下;
私人最终字符串值[];
公共CustomPasswordsList(上下文的背景下,的String []值)
{
超(背景下,R.layout.password_list_item,价值观);
this.context =背景;
this.values =值;
}
公共查看getView(INT位置,查看convertView,父母的ViewGroup)
{
LayoutInflater充气=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
查看rowView = inflater.inflate(R.layout.password_list_item,父母,假);
TextView的passwordTitle =(TextView中)rowView.findViewById(R.id.textView_passwordTitle);
ImageView的passwordIcon =(ImageView的)rowView.findViewById(R.id.imageView_passwordIcon);
passwordTitle.setText(值[位置]);
passwordIcon.setImageResource(R.drawable.facebook);
返回rowView;
}
}
passwords_list_item
< XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:weightSum =1
机器人:方向=横向>
< ImageView的
机器人:ID =@ + ID / imageView_passwordIcon
机器人:layout_height =60dp
机器人:contentDescription =@字符串/ imageView_passwordIcon
机器人:layout_width =60dp/>
<的TextView
机器人:ID =@ + ID / textView_passwordTitle
机器人:layout_height =WRAP_CONTENT
机器人:layout_width =0dp
机器人:以下属性来=10dp
机器人:paddingTop =15dp
机器人:TEXTSIZE =16SP
机器人:文字颜色=#000000
机器人:layout_weight =1/>
< / LinearLayout中>
使用 getActivity()
就够了
setListAdapter(新CustomPasswordsList(getActivity(),职称));
您需要重写 onItemCheckedStateChanged
公共无效onItemCheckedStateChanged(ActionMode模式,
INT位置,长的ID,布尔检查){
。最终诠释checkedCount = getListView()getCheckedItemCount();
//得到检查的项目数
从样品图纸@
Android的SDK-Linux的/样本/ Android为17 / ApiDemos / src目录/ COM /例子/安卓/原料药/查看/ List16
例:修改根据您的需求低于
公共类MainActivity扩展ListActivity {
的String [] GENRES =新的String [] {
行动,探险,动画,儿童,喜剧,
纪录片,戏剧,
外国,历史,独立报,浪漫,科幻,
电视,颤栗
};
@覆盖
保护无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
ListView的LV = getListView();
lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
lv.setMultiChoiceModeListener(新ModeCallback());
setListAdapter(新ArrayAdapter<字符串>(这一点,
android.R.layout.simple_list_item_activated_1,流派));
}
@覆盖
保护无效onPostCreate(包savedInstanceState){
super.onPostCreate(savedInstanceState);
getActionBar()setSubtitle(长preSS启动选项)。
}
私有类ModeCallback实现ListView.MultiChoiceModeListener {
公共布尔onCreateActionMode(ActionMode模式,菜单菜单){
MenuInflater充气= getMenuInflater();
inflater.inflate(R.menu.list_select_menu,菜单);
mode.setTitle(选择项);
返回true;
}
在prepareActionMode公共布尔(ActionMode模式,菜单菜单){
返回true;
}
公共布尔onActionItemClicked(ActionMode模式,菜单项项){
开关(item.getItemId()){
案例R.id.share:
Toast.makeText(MainActivity.this,共享+ getListView()。getCheckedItemCount()+
项目,Toast.LENGTH_SHORT).show();
mode.finish();
打破;
默认:
Toast.makeText(MainActivity.this,点击+ item.getTitle(),
Toast.LENGTH_SHORT).show();
打破;
}
返回true;
}
公共无效onDestroyActionMode(ActionMode模式){
}
公共无效onItemCheckedStateChanged(ActionMode模式,
INT位置,长的ID,布尔检查){
。最终诠释checkedCount = getListView()getCheckedItemCount();
开关(checkedCount){
情况下0:
mode.setSubtitle(空);
打破;
情况1:
mode.setSubtitle(一个项目入选);
打破;
默认:
mode.setSubtitle(选择+ checkedCount +项目);
打破;
}
}
}
}
list_select_menu.xml
<菜单的xmlns:机器人=http://schemas.android.com/apk/res/android>
<项目机器人:ID =@ + ID /股
机器人:标题=分享
机器人:图标=@机器人:可绘制/ ic_menu_share
机器人:showAsAction =总是/>
< /菜单>
快照
既然你怀疑它是否将与自定义适配器
编辑:
在RES /值-V11 / styles.xml
<资源>
<样式名称=AppTheme父=机器人:Theme.Holo.Light>< /风格>
<样式名称=激活父=AppTheme>
<项目名称=机器人:背景>安卓?ATTR / activatedBackgroundIndicator< /项目>
< /风格>
< /资源>
在根元素的自定义布局添加
风格=@风格/激活
I have created a custom listview
which has an ImageView
on the left and a TextView
on the right. And implementing a multi-selection Listview
with actionbar
context menu.
The problem is, when i long click on an item it didn't gets highlighted.
Here is how i setup the listview
in my ListFragment
PasswordsFragment.java
package mohd.itcs.safewallet;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.ActionMode;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AbsListView.MultiChoiceModeListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ListView;
public class PasswordsFragment extends ListFragment {
private String titles[] = { "item1", "item2", "item3" };
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new CustomPasswordsList(getActivity(), titles));
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
/*
* Setup Multiple Selection Mode
*/
getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
getListView().setOnItemLongClickListener(new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> listview, View item,
int position, long id) {
getListView().setItemChecked(position, true);
return true;
}
});
getListView().setMultiChoiceModeListener(new MultiChoiceModeListener() {
@Override
public boolean onActionItemClicked(ActionMode arg0, MenuItem arg1) {
// TODO Auto-generated method stub
return false;
}
/*
* Inflate Actionbar Menu for Passwords Multiple Selection
*/
@Override
public boolean onCreateActionMode(ActionMode arg0, Menu menu) {
getActivity().getMenuInflater().inflate(
R.menu.passwords_context_menu, menu);
return true;
}
@Override
public void onDestroyActionMode(ActionMode arg0) {
}
@Override
public boolean onPrepareActionMode(ActionMode arg0, Menu arg1) {
return false;
}
@Override
public void onItemCheckedStateChanged(ActionMode actionMode,
int position, long id, boolean arg3) {
/*
* Change Title bar to number of selection
*/
int checkedItems = getListView().getCheckedItemCount();
actionMode.setTitle(String.valueOf(checkedItems) + " Selected");
}
});
}
}
CustomPasswordsList.java
package mohd.itcs.safewallet;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class CustomPasswordsList extends ArrayAdapter<String> {
private final Context context;
private final String values[];
public CustomPasswordsList(Context context, String[] values)
{
super(context, R.layout.password_list_item, values);
this.context = context;
this.values = values;
}
public View getView(int position, View convertView, ViewGroup parent)
{
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.password_list_item, parent, false);
TextView passwordTitle = (TextView) rowView.findViewById(R.id.textView_passwordTitle);
ImageView passwordIcon = (ImageView) rowView.findViewById(R.id.imageView_passwordIcon);
passwordTitle.setText(values[position]);
passwordIcon.setImageResource(R.drawable.facebook);
return rowView;
}
}
passwords_list_item
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1"
android:orientation="horizontal" >
<ImageView
android:id="@+id/imageView_passwordIcon"
android:layout_height="60dp"
android:contentDescription="@string/imageView_passwordIcon"
android:layout_width="60dp" />
<TextView
android:id="@+id/textView_passwordTitle"
android:layout_height="wrap_content"
android:layout_width="0dp"
android:paddingLeft="10dp"
android:paddingTop="15dp"
android:textSize="16sp"
android:textColor="#000000"
android:layout_weight="1" />
</LinearLayout>
using getActivity()
is enough
setListAdapter(new CustomPasswordsList(getActivity(), titles));
You need to override onItemCheckedStateChanged
public void onItemCheckedStateChanged(ActionMode mode,
int position, long id, boolean checked) {
final int checkedCount = getListView().getCheckedItemCount();
// get checked items count
Drawing from the samples @
android-sdk-linux/samples/android-17/ApiDemos/src/com/example/android/apis/view/List16
Example : Modify the below according to your needs
public class MainActivity extends ListActivity {
String[] GENRES = new String[] {
"Action", "Adventure", "Animation", "Children", "Comedy",
"Documentary", "Drama",
"Foreign", "History", "Independent", "Romance", "Sci-Fi",
"Television", "Thriller"
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ListView lv = getListView();
lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
lv.setMultiChoiceModeListener(new ModeCallback());
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_activated_1, GENRES));
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
getActionBar().setSubtitle("Long press to start selection");
}
private class ModeCallback implements ListView.MultiChoiceModeListener {
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.list_select_menu, menu);
mode.setTitle("Select Items");
return true;
}
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return true;
}
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
switch (item.getItemId()) {
case R.id.share:
Toast.makeText(MainActivity.this, "Shared " + getListView().getCheckedItemCount() +
" items", Toast.LENGTH_SHORT).show();
mode.finish();
break;
default:
Toast.makeText(MainActivity.this, "Clicked " + item.getTitle(),
Toast.LENGTH_SHORT).show();
break;
}
return true;
}
public void onDestroyActionMode(ActionMode mode) {
}
public void onItemCheckedStateChanged(ActionMode mode,
int position, long id, boolean checked) {
final int checkedCount = getListView().getCheckedItemCount();
switch (checkedCount) {
case 0:
mode.setSubtitle(null);
break;
case 1:
mode.setSubtitle("One item selected");
break;
default:
mode.setSubtitle("" + checkedCount + " items selected");
break;
}
}
}
}
list_select_menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/share"
android:title="share"
android:icon="@android:drawable/ic_menu_share"
android:showAsAction="always" />
</menu>
Snap shot
Since you are doubting whether it would work with custom adapter
Edit:
under res/values-v11/styles.xml
<resources>
<style name="AppTheme" parent="android:Theme.Holo.Light"></style>
<style name="activated" parent="AppTheme">
<item name="android:background">?android:attr/activatedBackgroundIndicator</item>
</style>
</resources>
In the custom layout for the root element add
style="@style/activated"
这篇关于当长按突出显示自定义列表视图项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!