Android 以双窗格模式在listview中维护所选内容
我试图创建一个类似gmail的界面,用户点击左边的列表,然后在右边显示一些东西。我已经到了这样一个地步:我在左边有一个列表,用户可以单击,项目就会突出显示(我是初学者)。问题是,如果用户单击右侧的空白部分,突出显示的项目将不再突出显示Android 以双窗格模式在listview中维护所选内容,android,listview,Android,Listview,我试图创建一个类似gmail的界面,用户点击左边的列表,然后在右边显示一些东西。我已经到了这样一个地步:我在左边有一个列表,用户可以单击,项目就会突出显示(我是初学者)。问题是,如果用户单击右侧的空白部分,突出显示的项目将不再突出显示 public class profileListFragment extends ListFragment { String[] countries = new String[] {"USA", "China"}; @Override
public class profileListFragment extends ListFragment {
String[] countries = new String[] {"USA", "China"};
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
ArrayAdapter<String> adapter = new ArrayAdapter<String>(inflater.getContext(),
android.R.layout.simple_list_item_1,countries);
setListAdapter(adapter);
// View listView = getActivity().findViewById(R.id.list));
View retView = inflater.inflate(R.layout.fragment_load_profile_list_layout,container, false);
ListView lv = (ListView) retView.findViewById(android.R.id.list);
lv.setSelector(android.R.color.holo_orange_light);
lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
// lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
return retView;
// return super.onCreateView(inflater, container, savedInstanceState);
}
@Override
public void onPause(){
}
}
public class MyAdapter extends BaseAdapter {
private LayoutInflater mInflater;
private int mPositionSelected;
private Context mContext;
static class MyViewHolder {
TextView yearMonth;
TextView number;
}
public MyAdapter(Context context) {
mContext = context;
mInflater = LayoutInflater.from(context);
}
public void setPositionSelected(int positionSelected) {
mPositionSelected = positionSelected;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
MyViewHolder myViewHolder;
if (convertView == null) { // if it's not recycled, initialize some attributes
convertView = mInflater.inflate(R.layout.my_layout, null);
myViewHolder = new MyViewHolder();
myViewHolder.yearMonth = (TextView) convertView.findViewById(R.id.yearMonthInspections);
myViewHolder.number = (TextView) convertView.findViewById(R.id.numberOfInspections);
convertView.setTag(myViewHolder);
} else {
myViewHolder = (MyViewHolder) convertView.getTag();
}
myViewHolder.yearMonth.setText(...);
myViewHolder.number.setText(...);
// set background color to the selected position.
// this is used rotations. The position is maintained here and
// in the fragment
if (mPositionSelected == position) {
convertView.setBackgroundColor(Color.BLUE);
}
return convertView;
}
}
profile_list_selector.xml位于“drawable”文件夹中,如下所示:
lv.setSelector(R.drawable.profile_list_selector);
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_enabled="false"
android:drawable="@android:color/holo_green_dark" />
<item android:state_pressed="true"
android:drawable="@android:color/holo_blue_dark" />
<item android:state_focused="true" android:state_enabled="true"
android:drawable="@android:color/holo_blue_bright" />
<item android:state_enabled="true"
android:drawable="@android:color/holo_orange_dark" />
</selector>
我现在注意到的是,在右边的屏幕上,只有当我点击右边的文本框时,高亮显示才会消失。。。如果我点击复选框,突出显示的项目将被保留。对这一切完全困惑不幸的是,目前还没有简单的答案 您必须执行一些类似于自定义适配器的操作,以不同的方式绘制当前选定的项。您还需要跟踪所选项目的状态 有关可能的解决方案,请参阅本博客文章
这个问题很老了,但我最近遇到了这个问题。我有一个两窗格的平板电脑布局。一个窗格具有项目列表视图,第二个窗格显示第一个窗格中选择的项目。当ListView中的某个项目被选中时,我希望将其标记为选中,并更改第二个窗格。当用户旋转设备时,我希望标记当前选择。以下是我的解决方案: 在我的列表片段中,我保留一个位置。这将保存在SavedInstanceState上,并在onCreate中还原。该位置在单击期间更新,并传递给适配器。在onListItemClick中,我也会相应地更改视图的背景色
public class MyListFragment extends ListFragment {
private static final String LIST_ITEM_SELECTED = "list_item_selected";
// keep the item selected to handle rotation
private int mPositionSelected = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState != null) {
mPositionSelected = savedInstanceState.getInt(LIST_ITEM_SELECTED, 0);
}
mAdapter = new MyAdapter(getActivity());
mAdapter.setPositionSelected(mPositionSelected);
setListAdapter(mAdapter);
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
if (Utils.isTablet(getActivity())) {
outState.putInt(LIST_ITEM_SELECTED, mPositionSelected);
}
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
// If this is a tablet layout the inspections list is visible.
// so send an event to change them.
if (Utils.isTablet(getActivity())) {
// set the old view's bg back to black
l.getChildAt(mPositionSelected).setBackgroundColor(Color.BLACK);
// set the selected view's bg
v.setBackgroundColor(Color.BLUE);
// keep the selected position
mPositionSelected = position;
// tell the adapter the currently selected position
mInspectionsByMonthAdapter.setPositionSelected(mPositionSelected);
// let the other fragment know the items it should display
EventBus.getInstance().post(...);
} else {
// else, we need to navigate to a new fragment.
}
}
}
在适配器中,我还保持位置处于选中状态。旋转设备时,在getView方法中,所选项目将高亮显示
public class profileListFragment extends ListFragment {
String[] countries = new String[] {"USA", "China"};
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
ArrayAdapter<String> adapter = new ArrayAdapter<String>(inflater.getContext(),
android.R.layout.simple_list_item_1,countries);
setListAdapter(adapter);
// View listView = getActivity().findViewById(R.id.list));
View retView = inflater.inflate(R.layout.fragment_load_profile_list_layout,container, false);
ListView lv = (ListView) retView.findViewById(android.R.id.list);
lv.setSelector(android.R.color.holo_orange_light);
lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
// lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
return retView;
// return super.onCreateView(inflater, container, savedInstanceState);
}
@Override
public void onPause(){
}
}
public class MyAdapter extends BaseAdapter {
private LayoutInflater mInflater;
private int mPositionSelected;
private Context mContext;
static class MyViewHolder {
TextView yearMonth;
TextView number;
}
public MyAdapter(Context context) {
mContext = context;
mInflater = LayoutInflater.from(context);
}
public void setPositionSelected(int positionSelected) {
mPositionSelected = positionSelected;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
MyViewHolder myViewHolder;
if (convertView == null) { // if it's not recycled, initialize some attributes
convertView = mInflater.inflate(R.layout.my_layout, null);
myViewHolder = new MyViewHolder();
myViewHolder.yearMonth = (TextView) convertView.findViewById(R.id.yearMonthInspections);
myViewHolder.number = (TextView) convertView.findViewById(R.id.numberOfInspections);
convertView.setTag(myViewHolder);
} else {
myViewHolder = (MyViewHolder) convertView.getTag();
}
myViewHolder.yearMonth.setText(...);
myViewHolder.number.setText(...);
// set background color to the selected position.
// this is used rotations. The position is maintained here and
// in the fragment
if (mPositionSelected == position) {
convertView.setBackgroundColor(Color.BLUE);
}
return convertView;
}
}
我希望这能有所帮助。它没有被取消选择,只是没有突出显示,我不知道有什么简单的东西,比如keepSelectedHighlightOnFocusChange或类似的东西。我知道的唯一方法是在自定义适配器中自己绘制,这就是我所展示的示例的要点。我看看能不能找到更具体一点的。很快回来…你真是太好了,非常感谢。。。我和一个朋友谈过,他提到了选择器。。。你怎么认为?这就是我现在正在研究的。。