Search in sources :

Example 1 with Chat

use of me.yokeyword.sample.demo_wechat.entity.Chat in project Fragmentation by YoKeyword.

the class WechatFirstTabFragment method initDatas.

private List<Chat> initDatas() {
    List<Chat> msgList = new ArrayList<>();
    String[] name = new String[] { "小黄", "小李", "小张", "老王", "小马" };
    String[] chats = new String[] { "小黄的消息", "小李的消息", "小张的消息", "老王的消息", "小马的消息" };
    for (int i = 0; i < 15; i++) {
        int index = (int) (Math.random() * 5);
        Chat chat = new Chat();
        chat.name = name[index];
        chat.message = chats[index];
        msgList.add(chat);
    }
    return msgList;
}
Also used : ArrayList(java.util.ArrayList) Chat(me.yokeyword.sample.demo_wechat.entity.Chat)

Example 2 with Chat

use of me.yokeyword.sample.demo_wechat.entity.Chat in project Fragmentation by YoKeyword.

the class WechatFirstTabFragment method onLazyInitView.

@Override
public void onLazyInitView(@Nullable Bundle savedInstanceState) {
    super.onLazyInitView(savedInstanceState);
    mRefreshLayout.setOnRefreshListener(this);
    mRecy.setLayoutManager(new LinearLayoutManager(_mActivity));
    mRecy.setHasFixedSize(true);
    final int space = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 0.5f, getResources().getDisplayMetrics());
    mRecy.addItemDecoration(new RecyclerView.ItemDecoration() {

        @Override
        public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
            outRect.set(0, 0, 0, space);
        }
    });
    mAdapter = new ChatAdapter(_mActivity);
    mRecy.setAdapter(mAdapter);
    mRecy.addOnScrollListener(new RecyclerView.OnScrollListener() {

        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);
            mScrollTotal += dy;
            if (mScrollTotal <= 0) {
                mInAtTop = true;
            } else {
                mInAtTop = false;
            }
        }
    });
    mAdapter.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(int position, View view, RecyclerView.ViewHolder vh) {
            // 因为启动的MsgFragment是MainFragment的兄弟Fragment,所以需要MainFragment.start()
            // 这里我使用EventBus通知父MainFragment处理跳转(接耦),
            EventBus.getDefault().post(new StartBrotherEvent(MsgFragment.newInstance(mAdapter.getMsg(position))));
        // 也可以像使用getParentFragment()的方式,拿到父Fragment的引用来操作 (不建议)
        //              ((MainFragment) getParentFragment()).startMsgBrother(MsgFragment.newInstance());
        }
    });
    List<Chat> chatList = initDatas();
    mAdapter.setDatas(chatList);
}
Also used : Rect(android.graphics.Rect) OnItemClickListener(me.yokeyword.sample.demo_wechat.listener.OnItemClickListener) LinearLayoutManager(android.support.v7.widget.LinearLayoutManager) RecyclerView(android.support.v7.widget.RecyclerView) View(android.view.View) StartBrotherEvent(me.yokeyword.sample.demo_wechat.event.StartBrotherEvent) Chat(me.yokeyword.sample.demo_wechat.entity.Chat) RecyclerView(android.support.v7.widget.RecyclerView) ChatAdapter(me.yokeyword.sample.demo_wechat.adapter.ChatAdapter)

Example 3 with Chat

use of me.yokeyword.sample.demo_wechat.entity.Chat in project Fragmentation by YoKeyword.

the class ChatAdapter method onBindViewHolder.

@Override
public void onBindViewHolder(VH holder, int position) {
    Chat item = mItems.get(position);
    holder.tvName.setText(item.name);
    holder.tvMsg.setText(item.message);
    holder.tvTime.setText("昨天");
}
Also used : Chat(me.yokeyword.sample.demo_wechat.entity.Chat)

Aggregations

Chat (me.yokeyword.sample.demo_wechat.entity.Chat)3 Rect (android.graphics.Rect)1 LinearLayoutManager (android.support.v7.widget.LinearLayoutManager)1 RecyclerView (android.support.v7.widget.RecyclerView)1 View (android.view.View)1 ArrayList (java.util.ArrayList)1 ChatAdapter (me.yokeyword.sample.demo_wechat.adapter.ChatAdapter)1 StartBrotherEvent (me.yokeyword.sample.demo_wechat.event.StartBrotherEvent)1 OnItemClickListener (me.yokeyword.sample.demo_wechat.listener.OnItemClickListener)1