在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,大家可以去了解一些,欢迎交流

  • 4
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值