android开发环境

基于Bmob的即时通信(11)朋友圈界面

2019-01-25  本文已影响36人  JYangkai

废话

今天我们来学习编写朋友圈页面

一般的聊天软件都会有好友分享动态的一个页面,比如QQ的QQ空间、微信的朋友圈,我们也可以做一个类似于朋友圈的页面,让我们开始吧

创建Fragment

public class PostFragment extends Fragment {

    private View mView;

    private Context mContext;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mContext=getContext();
        if(mView==null){
            mView=LayoutInflater.from(mContext).inflate(R.layout.fragment_post,container,false);
            fbv();
            init();
            event();
        }

        ViewGroup parent=(ViewGroup)mView.getParent();
        if(parent!=null){
            parent.removeView(mView);
        }

        return mView;
    }
}

我们的朋友圈页面也是一个Fragment,这里我们创建一个名为PostFragment的Fragment,同样,这个页面也是懒加载,也是在onCreateView方法里加载布局,然后后面跟着三个方法

下面我们一一来看

fbv

/**
 * 获取控件
 */
private void fbv(){
    mSwipeRefreshLayout = mView.findViewById(R.id.fragment_post_swipe_refresh_layout);
    mRecyclerView = mView.findViewById(R.id.fragment_post_recycler_view);
    mNoPostText=mView.findViewById(R.id.fragment_post_no_post_text);
}

这个方法获取所有的控件

init

/**
 * 初始化
 */
private void init(){
    initSwipe();
    initPost();
}

这个方法初始化下拉控件和朋友圈列表

initSwipe

private void initSwipe() {
    mSwipeRefreshLayout.setColorSchemeResources(R.color.colorPrimary);
}

initPost

private void initPost() {
    LinearLayoutManager layoutManager = new LinearLayoutManager(mContext);
    mRecyclerView.setLayoutManager(layoutManager);
    mAdapter=new PostAdapter(mPostList);
    mRecyclerView.setAdapter(mAdapter);
}

这个方法初始化我们的朋友圈列表

event

/**
 * 绑定事件
 */
private void event(){
    mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            getData();
        }
    });
}

这个方法为下拉控件绑定下拉事件

onResume

@Override
public void onResume() {
    super.onResume();
    mSwipeRefreshLayout.setRefreshing(true);
    getData();
}

同样,我们还需要重写onResume方法

getData

private void getData(){
    BmobQuery<Post> query=new BmobQuery<>();
    query.include("user");
    query.findObjects(new FindListener<Post>() {
        @Override
        public void done(List<Post> list, BmobException e) {
            mSwipeRefreshLayout.setRefreshing(false);
            if(e==null){
                if(list.size()!=0){
                    mNoPostText.setVisibility(View.GONE);
                    for(Post post:list){
                        if(!mPostList.contains(post)){
                            mPostList.add(0,post);
                            mAdapter.notifyDataSetChanged();
                        }
                    }
                }else{
                    mNoPostText.setVisibility(View.VISIBLE);
                }
            }else{
                mNoPostText.setVisibility(View.VISIBLE);
            }
        }
    });
}

最后,这是我们的获取朋友圈数据方法,这个方法在onResume和用户下拉列表的时候会调用,我们可以看到,在这个方法里,我们做了一个数据唯一性的判断,也就是这句代码:

!mPostList.contains(post)

这句代码表示:如果列表里已经有了,就不需要再添加进来了

嗯,今天就到此为止。

上一篇下一篇

猜你喜欢

热点阅读