在fragment中引用ListView-ListFragment的使用

前言:這個是之前那個LIstFragment的重新排版,已經把所以使用的代碼都羅列出來了,比較簡單,就不過多贅述了,只有幾個關鍵地方已經提及,需要可以複製過去看看

整體流程和使用ListView的流程差不多,首先是創建一個list_item.xml文件,用來表示要被複用的條目,然後是Food.java-需要在List中展示的對象,然後是ListMainAdapter.xml:用於對item的複用以及Food對象在item中的填充,然後就是存放ListView的Fragment,這裏使用ListFragment。最後是把fragment放入到Activity中

List的複用圖大概是這樣的:

在這裏插入圖片描述

1. 所以首先是list_item.xml
這裏的條目按自己需要的格式編輯,這裏我就採用最簡單的,減少不必要的部分
<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:id="@+id/list_food"
        android:layout_width="match_parent"
        android:layout_height="30dp"/>

</LinearLayout>
2. Food.java
一個簡單的Javabean代表想要展示的對象
public class Food {

    private String mName;

    public Food(String name){
        mName = name;
    }

    public String getName() {
        return mName;
    }

    public void setName(String name) {
        mName = name;
    }

}

3. ListMainAdapter.xml
填充的適配器
public class ListMainAdapter extends ArrayAdapter<Food> {

    private int resourceId;
    private Context context;

    public ListMainAdapter(@NonNull Context context, int resource, @NonNull List<Food> objects) {
        super(context, resource, objects);
        this.context = context;
        this.resourceId = resource;
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        //獲取food對象
        Food food = getItem(position);
        View view = LayoutInflater.from(context).inflate(resourceId,null);
        TextView textView = view.findViewById(R.id.list_food);
        textView.setText(food.getName());
        return view;
    }
}

4. fragment_main.xml

這裏主要就是注意一下id爲固定的id

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <!-- ListFragment對應的android:id值固定爲"@id/android:list" -->
    <ListView
        android:id="@id/android:list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:drawSelectorOnTop="false"/>
</LinearLayout>
5. MainFragment.java
Fragment需要繼承至ListFragment,優化可以加入ViewHolder的使用
public class MainFragment extends ListFragment {

    List<Food> mFoodList;
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mFoodList = new ArrayList<>();
        initFood();
        ListMainAdapter adapter = new ListMainAdapter(getContext(),R.layout.list_item,mFoodList);
        this.setListAdapter(adapter);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_main,container,false);
    }

    private void initFood(){
        mFoodList.add(new Food("apple"));
        mFoodList.add(new Food("banana"));
        mFoodList.add(new Food("hot"));
    }
}

6. 最後就是把fragment加入到Activity中

MainActivity.java

public class MainActivity extends AppCompatActivity {

    MainFragment mMainFragment;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mMainFragment = new MainFragment();
        getSupportFragmentManager().beginTransaction().add(R.id.main_container,mMainFragment).commit();
    }
}

activity_main.xml

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <FrameLayout
        android:id="@+id/main_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>

大概過程就是這樣,可能一些表達不是很準確,以後會慢慢更改完善,不過代碼一些部分寫的比較簡單,沒有經過優化,但優化上面和一般的差不多,這裏就不過多寫了,如果不能看懂哪裏可以留言一下,列表這方面還是比較推薦recycleview,大家可以去了解一些,歡迎交流

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章