0

我正在尝试创建一个类似 gmail 的界面,用户单击左侧的列表并在右侧显示一些内容。我已经到了左侧有一个列表的地步,用户可以单击并突出显示该项目(我是初学者)。问题是,如果用户单击暂时空白的右侧,突出显示的项目将不再突出显示。

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(){

    }
}

如果您需要查看更多代码,请告诉我。提前致谢

编辑

我已将代码编辑如下:

lv.setSelector(R.drawable.profile_list_selector);

profile_list_selector.xml 位于“drawable”文件夹中,如下所示:

<?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>

我现在注意到的是,在右侧屏幕上,只有当我单击右侧的文本框时,突出显示才会丢失……如果我单击复选框,则突出显示的项目会被保留。完全对这一切感到困惑

4

2 回答 2

1

这个问题很老,但我最近遇到了这个问题。我有一个两窗格的平板电脑布局。一个窗格有一个 ListView 项目,第二个窗格显示在第一个窗格中选择的项目。当 ListView 中的一个项目被选中时,我想将其标记为,并更改第二个窗格。当用户旋转设备时,我想保留当前选择的标记。这是我的解决方案:

在我的列表片段中,我保留了一个位置。这保存在 onSavedInstanceState 并在 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 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;
    }
}

我希望这有帮助。

于 2014-06-15T02:16:48.587 回答
0

不幸的是,AFAIK 没有简单的答案。

您必须执行类似自定义适配器的操作,以不同方式绘制当前选定的项目。您还需要跟踪所选项目的状态。

有关可能的解决方案,请参阅此博客文章。

http://bestsiteinthemultiverse.com/2009/12/android-selected-state-listview-example/

于 2012-11-11T19:28:38.710 回答