【问题标题】:Multi-pane layout多窗格布局
【发布时间】:2012-06-18 23:16:50
【问题描述】:

我想使用 2 个面板为平板电脑创建一个应用程序。左边一个作为菜单,右边一个作为内容演示者。事实上,我期望的行为类似于 Gmail 应用程序中的行为。

我目前正在使用 2 个片段。一个基本上是带有房间名称和一些按钮的 ListView。另一个(内容)包含有关所选房间的一些信息。

我在左侧面板中选择的房间应该保持突出显示 - 就像在 Gmail 应用中一样。我尝试了好几个小时来使用选择器来实现这个效果,但是失败了。

此外,我发现关于这种方法的 2 种不同意见..

1) "Do not try to keep the focus or selection in touch mode"

2) "In general, use the pane on the right to present more information about the item you selected in the left pane. Make sure to keep the item in the left pane selected in order to establish the relationship between the panels."

你能告诉我如何做到这一点(只是为了使所选项目与其他项目区分开来)? 也许 ListView 不是这里最好的方法?

更新

感谢 Christoph Eberhardt 的回答,我创建了一个运行良好的示例项目。可用on github.

【问题讨论】:

  • 在我的旧答案中更正了示例:第一个答案是从我的代码中复制的,我在 ListFragment 上使用过,它确实有效。我编辑的 anwser 现在应该可以工作了

标签: android android-layout


【解决方案1】:

在res/drawable/list_item_selector.xml中创建一个xml文件

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_selected="true" android:drawable="@drawable/list_pressed" />
    <item android:drawable="@drawable/list_default" />
</selector>

为列表元素制作一个自己的xml文件res/layout/list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:gravity="center_vertical"
    android:minHeight="?android:attr/listPreferredItemHeight"
    android:paddingLeft="20dp"
    android:background="@drawable/list_item_selector"
/>

然后在创建列表适配器时:

setListAdapter(new CustomAdapter<String>(getActivity(),
            R.layout.list_item, stringArray));

现在您所要做的就是提供可绘制对象 list_default 和 list_pressed 和 list_altered

CustomAdpater 看起来像:

package com.test.listview_keep_selected;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.StateListDrawable;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class CustomAdapter<T> extends ArrayAdapter<T> {

    private Context m_cContext;

    public CustomAdapter(Context context, int textViewResourceId,
            T[] objects) {

        super(context, textViewResourceId, objects);
        this.m_cContext = context;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final View returnView = super.getView(position, convertView, parent);
        final ListView listView  = (ListView) ((Activity) m_cContext).findViewById(R.id.listViewTest);
        returnView.setOnClickListener(new OnClickListener(
                ) {


            @Override
            public void onClick(View v) {
                for(int i = 0; i< listView.getChildCount(); i++)
                    listView.getChildAt(i).setSelected(false);
                StateListDrawable states = new StateListDrawable();
                states.addState(new int[] {android.R.attr.state_selected},
                        m_cContext.getResources().getDrawable(R.drawable.list_pressed));
                states.addState(new int[] { },
                        m_cContext.getResources().getDrawable(R.drawable.list_altered));
                v.setBackgroundDrawable(states);
                v.setSelected(true);
            }
        });
        return returnView;
    }

}

编辑:

如果你还没有:

listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);

编辑: 不要使用普通的 ListView 设置选择模式,只有当你有例如时才有意义。我在代码中使用的 ListFragment

现在还不完美,不过以后玩一玩应该就好了。

还有可能有帮助的是:ListView item background via custom selector

编辑:

现在它应该可以按你的意愿工作了^^

【讨论】:

  • 我听从了你的指示。这不会使所选项目保持突出显示。事实上,它在被按下时甚至不会被突出显示。 :(这是我的结果:github.com/gisek/ListView_test我错过了什么吗?
  • 首先感谢您的宝贵时间。我认为您的 list_item.xml 缺少一行 [android:background="@drawable/list_item_selector"]。现在它工作得更好了。列表项是蓝色的,当我触摸其中一项时,它会变成红色。但它仍然没有对我来说至关重要的功能。当我释放该项目时,它会变成蓝色,但在我按下另一个之前应该保持红色。
  • mmh 是的,你写了那个,写你自己的适配器怎么样,捕捉点击事件,然后改变 listitem 视图的背景,应该是几分钟的事情
  • 在屏幕截图中,您可以看到列表顶部的项目是 azure。我想只要用户正在阅读相关的电子邮件,它就会保持天蓝色。我需要类似的东西。我按“厨房”它变成红色并保持红色,直到我读完有关厨房的详细信息。比我按“客厅”。厨房变回蓝色。客厅变红了。以此类推。
  • 最后,我想这就是你想要的
猜你喜欢
  • 2013-10-27
  • 1970-01-01
  • 1970-01-01
  • 2014-04-01
  • 1970-01-01
  • 2015-11-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多