我应该使用什么来在Fragment中创建自定义ListView以满足我的需要?

时间:2015-09-26 18:23:10

标签: android android-fragments android-listview

我一直在创建新闻应用程序。我用this tutorial创建了漂亮的导航抽屉。它为导航列表中的每个类别使用片段。我将使用提供的API来获取新闻(采用JSON格式),我需要自定义列表视图来显示新闻列表。单击列表项时,将打开新视图以显示新闻详细信息。我找到了几个教程,但它们都没有用于片段。请告诉我这样做的方法。

My Layouts My Fragments

我是Android编程的新手。这是我的第一个项目,虽然它很难成为第一个项目。但我必须这样做。请帮助!

事先感谢!

2 个答案:

答案 0 :(得分:1)

假设您知道如何从适用于活动的教程中创建适配器和自定义行布局。微小的修改将帮助您使用碎片。

您的片段应如下所示:

public class NewFragment extends Fragment{

    CustomAdapter listAdapter;
    ListView listView;
    ArrayList<Data> yourData; // Should be filled with data from your JSONParser;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        ...

        /*
        * Use your custom adapter. in this example the adapter need 
        * the context and an array that contains your data
        */
        listAdapter = new CustomAdapter(getActivity(),yourData);  

        ...

    }

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

        View view = inflater.inflate(R.layout.new_fragment, container, false);

        listView = (LsitView)view.findViewById(R.id.your_list);

        listView.setAdapter(listAdapter);

        ...


    }

}

答案 1 :(得分:1)

您可以使用此ListFragment

这是一个完全自定义的示例:

activity_main.xml中

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:id="@+id/frameLayout"/>
</RelativeLayout>

mylist.xml

<?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/android:list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#00FF00"
        android:layout_weight="1"
        android:drawSelectorOnTop="false"/>

    <TextView android:id="@id/android:empty"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#FF0000"
        android:text="No data"/>

</LinearLayout>

myitem.xml

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

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text=""
        android:id="@+id/txt_mytext" />
</LinearLayout>

MainActivity.java

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Fragment fr = mListFragment.newInstance();
        FragmentManager fm = getFragmentManager();
        fm.beginTransaction().add(R.id.frameLayout, fr, "tag").commit();
    }
}

mListFragment.java

public class mListFragment extends ListFragment {
    public static mListFragment newInstance() {
        mListFragment f = new mListFragment();
        //Bundle args = new Bundle();

        //f.setArguments(args);
        return f;
    }
    public mListFragment() {}

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        String[] strings = {"a", "b", "c"};
        ArrayAdapter<String> adapter = new LstAdapter(
                getActivity(), R.layout.myitem,
                strings);
        setListAdapter(adapter);
        return inflater.inflate(R.layout.mylist, null);
    }

    public class LstAdapter extends ArrayAdapter<String> {
        private String[] mArray;

        public LstAdapter(Context context, int textViewResourceId, String[] mList) {
            super(context, textViewResourceId, mList);
            mArray = mList;
        }

        @Override
        public int getCount() {
            return mArray.length;
        }

        @Override
        public String getItem(int position) {
            return mArray[position];
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View v = convertView;
            if (v == null) {
                LayoutInflater vi = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                v = vi.inflate(R.layout.myitem, null);
            }

            TextView tvName = (TextView) v.findViewById(R.id.txt_mytext);
            tvName.setText(mArray[position]);

            return v;
        }
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        Log.d("123", position + "");
        super.onListItemClick(l, v, position, id);
    }
}