【问题标题】:Fragment Class with ListView?带有 ListView 的片段类?
【发布时间】:2013-12-30 05:40:54
【问题描述】:

是否可以在 Fragment 类中自定义 ListView

这是我的片段类,其中fragment_find_people 只是一个空的XML

public class FindPeopleFragment extends Fragment {

public FindPeopleFragment(){}


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

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

    return rootView;
}

}

现在,我已经有了一个 customListView,但它只适用于 Activity 类。我怎样才能把它转给Fragment

我的 listview_main xml

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" 
    android:id="@+id/listmain" >

<ListView
    android:id="@+id/listview"
    android:layout_marginTop="10dp"
    android:background="@drawable/bg"
    android:divider="#9c9c9c"
    android:dividerHeight="5dp"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" /></RelativeLayout>

【问题讨论】:

  • @EugenMartynov 我使用的是片段类而不是活动类
  • @user3138250 发布更多代码。你为此做了什么?
  • @user3138250 你有没有试过用一些数据绑定listview?
  • 我在 Activity 类中尝试过,但在 Fragment 类中还没有尝试过

标签: android listview android-fragments android-fragmentactivity


【解决方案1】:

类上下文替换为getActivity 这是唯一的区别并替换视图而不是活动:

View rootView = inflater.inflate(R.layout.fragment_find_people, container, false);
//now you must initialize your list view
Listview listview =(Listview)rootView .findViewById(R.id.your_listview);

//EDITED Code 
String[] items = new String[] {"Item 1", "Item 2", "Item 3"};
ArrayAdapter<String> adapter =
new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, items); 

listview.setAdapter(adapter);  

//To have custom list view use this : you must define CustomeAdapter class
//listview.setadapter(new CustomeAdapter(getActivity()));
//getActivty is used instead of Context
return view;

【讨论】:

【解决方案2】:

在 onCreateView 方法中首先定义你的布局。

RelativeLayout rl=(RelativeLayout)inflater.inflate(R.layout.status,container, false);
        list=(ListView) rl.findViewById(R.id.list1);
         itemList = new ArrayList<HashMap<String, String>>();

          // fill the defined arraylist using and asynctask or any other method

           adapter=new Customadapt(context.getActivity(), itemList);        
           list.setAdapter(adapter);
           return rl;

【讨论】:

    【解决方案3】:

    您也可以通过编程方式创建自定义列表视图。例如:

     public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
    
    
        return new CustomListView(container.getContext());
    }
    

    【讨论】:

      【解决方案4】:

      在 onCreateView(...) 方法中,像这样调用你的 CustomListAdapter

      adapter = new CustomListAdapter(getActivity());
      customListView.setAdapter(adapter);
      

      你的 CustomListAdapter 如下,例如

      private class CustomListAdapter extends BaseAdapter {
      
      
          LayoutInflater inflater;
          public Context context;
      
          public CustomListAdapter(Context context) {
      
              this.context = context;
              inflater = LayoutInflater.from(this.context);
      
          }
      
       .......
       .......
      }
      

      【讨论】:

        【解决方案5】:

        首先,是的,这是可能的。方法如下。

        与所有其他资源一样,有两种声明它们的方法。以编程方式和通过 XML 布局文件。我将解释如何使用 XML,因为它是我最常使用并且我认为最好的。

        在您的fragment_find_people XML 中,声明您的布局正常。在那个布局中,我假设至少有一个LinearLayout,你需要添加一个ListView

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

        现在您可以使用以下方法实例化 ListView

        Listview listView =(ListView)rootView.findViewById(R.id.your_list_view_id);
        

        为了使用“相同的”ListView,我建议简单地将ListView 的代码从一个布局文件(用于您的Activity 的那个文件)复制到Fragment。您还可以将ListView 的声明分离到一个单独的 XML 中,如果您要大量重用它会更整洁,但对于一个小项目,只需复制它即可。

        【讨论】:

        • 我的错,这是一个错字。该类称为ListView(注意CamelCase)而不是Listview。我更正了。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-12-05
        • 1970-01-01
        • 1970-01-01
        • 2014-04-28
        相关资源
        最近更新 更多