【问题标题】:Adding a listview as on of the views that can be selected in a navigation drawer添加列表视图作为可以在导航抽屉中选择的视图
【发布时间】:2015-04-27 03:12:38
【问题描述】:

首先;我想为这个冗长的问题道歉。我虽然这是了解我正在尝试做的事情的唯一方法。反正。我有一个应用程序,它使用一个导航抽屉,它将为用户提供 3 个选择。我希望 Test1Fragment 成为一个列表视图。我似乎无法弄清楚如何做到这一点。请帮忙。

根据选择控制加载哪个片段的MainActivity方法:

    private void selectItem(int position) {
    // update the main content by replacing fragments

    Fragment fragment = null;

    switch (position) {
        case 0:
            fragment = new Test1Fragment();
            break;
        case 1:
            fragment = new Test2Fragment();
            break;
        case 2:
            fragment = new Test3Fragment();
            break;
        default:
            break;
    }

    if (fragment != null) {
        FragmentManager fragmentManager = getFragmentManager();
        fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();

        // update selected item and title, then close the drawer
        mDrawerList.setItemChecked(position, true);
        mDrawerList.setSelection(position);
        setTitle(mNavigationDrawerItemTitles[position]);
        mDrawerLayout.closeDrawer(mDrawerList);

    } else {
        // error in creating fragment
        Log.e("MainActivity", "Error in creating fragment");
    }
}

Test1Fragment:

public class Test1Fragment extends Fragment {


public Test1Fragment() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_create, container, false);

    return rootView;
}

fragment_create.xml:

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

<!--<TextView-->
    <!--android:id="@+id/txtLabel"-->
    <!--android:layout_width="wrap_content"-->
    <!--android:layout_height="wrap_content"-->
    <!--android:layout_centerInParent="true"-->
    <!--android:text="Create View"-->
    <!--android:textSize="16dp" />-->

<!--<ImageView-->
    <!--android:layout_width="wrap_content"-->
    <!--android:layout_height="wrap_content"-->
    <!--android:layout_below="@id/txtLabel"-->
    <!--android:layout_centerHorizontal="true"-->
    <!--android:layout_marginTop="10dp"-->
    <!--android:src="@drawable/ic_action_copy" />-->

    <ListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/listView"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true" />
</RelativeLayout>

我知道我缺少列表视图的代码,但这就是我想要的 xml 视图。

谢谢

【问题讨论】:

  • 发布您的R.layout.fragment_create

标签: android listview fragment navigation-drawer


【解决方案1】:

在您当前的实现中,您的布局代码应如下所示:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">

    <ListView
        android:id="@+id/layout_fragment_listView"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"/>

</LinearLayout>

然后在您的班级中,您必须用数据填充列表视图。您必须创建适配器并将数据附加到此适配器。然后适配器必须传递给listview。 快速示例:

public class Test1Fragment extends Fragment { 

//No need for empty constructor, it is made by default
//public Test1Fragment() { 
//} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_create, container, false);

   ListView listView =(ListView)rootView.findViewById(R.id.layout_fragment_listView);

   String[] items = { "Milk", "Cheese", "Cucumber", "Toilet", "Duck" };

   ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
                    android.R.layout.simple_list_item_1, items);

   listView.setAdapter(adapter);


    return rootView;
} 

这样,您将使用数组项中的字符串填充列表视图。 这是一个非常基本的示例,您还可以扩展基本适配器,并使用您自己的对象和视图填充它。

这是带有结果的屏幕截图:

【讨论】:

  • 我以前试过一次,但它不起作用。首先,我不能使用 findViewById (我假设因为该类是一个片段)。错误是“可以解析方法” 其次,我收到 arrayAdapter 代码(this, android.R.layout.simple_list_item_1, items)的错误“无法解析构造函数”;
  • 检查我的答案,我从“this”->“getActivity()”修复了构造函数,现在您只需在转换根布局后调用即可使用 findViewById。希望对您有所帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-15
  • 1970-01-01
相关资源
最近更新 更多