Search in sources :

Example 1 with StartBrotherEvent

use of me.yokeyword.sample.demo_wechat.event.StartBrotherEvent 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 2 with StartBrotherEvent

use of me.yokeyword.sample.demo_wechat.event.StartBrotherEvent in project Fragmentation by YoKeyword.

the class FirstPagerFragment method initView.

private void initView(View view) {
    EventBus.getDefault().register(this);
    mRecy = (RecyclerView) view.findViewById(R.id.recy);
    mRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.refresh_layout);
    mRefreshLayout.setOnRefreshListener(this);
    mAdapter = new PagerAdapter(_mActivity);
    mRecy.setHasFixedSize(true);
    LinearLayoutManager manager = new LinearLayoutManager(_mActivity);
    mRecy.setLayoutManager(manager);
    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 holder) {
            // 通知MainActivity跳转至CycleFragment
            EventBus.getDefault().post(new StartBrotherEvent(CycleFragment.newInstance(1)));
        }
    });
    // Init Datas
    List<String> items = new ArrayList<>();
    for (int i = 0; i < 20; i++) {
        String item = "联系人 " + i;
        items.add(item);
    }
    mAdapter.setDatas(items);
}
Also used : OnItemClickListener(me.yokeyword.sample.demo_wechat.listener.OnItemClickListener) ArrayList(java.util.ArrayList) LinearLayoutManager(android.support.v7.widget.LinearLayoutManager) RecyclerView(android.support.v7.widget.RecyclerView) View(android.view.View) PagerAdapter(me.yokeyword.sample.demo_wechat.adapter.PagerAdapter) StartBrotherEvent(me.yokeyword.sample.demo_wechat.event.StartBrotherEvent) RecyclerView(android.support.v7.widget.RecyclerView)

Example 3 with StartBrotherEvent

use of me.yokeyword.sample.demo_wechat.event.StartBrotherEvent in project Fragmentation by YoKeyword.

the class WechatThirdTabFragment method onLazyInitView.

@Override
public void onLazyInitView(@Nullable Bundle savedInstanceState) {
    super.onLazyInitView(savedInstanceState);
    mAdapter = new HomeAdapter(_mActivity);
    LinearLayoutManager manager = new LinearLayoutManager(_mActivity);
    mRecy.setLayoutManager(manager);
    mRecy.setAdapter(mAdapter);
    mAdapter.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(int position, View view, RecyclerView.ViewHolder vh) {
            // 通知MainActivity跳转至DetailFragment
            EventBus.getDefault().post(new StartBrotherEvent(DetailFragment.newInstance(mAdapter.getItem(position).getTitle())));
        }
    });
    // Init Datas
    List<Article> articleList = new ArrayList<>();
    for (int i = 0; i < 3; i++) {
        Article article = new Article(mTitles[i], mContents[i]);
        articleList.add(article);
    }
    mAdapter.setDatas(articleList);
    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) {
                mAtTop = true;
            } else {
                mAtTop = false;
            }
        }
    });
}
Also used : OnItemClickListener(me.yokeyword.sample.demo_wechat.listener.OnItemClickListener) Article(me.yokeyword.sample.demo_wechat.entity.Article) ArrayList(java.util.ArrayList) LinearLayoutManager(android.support.v7.widget.LinearLayoutManager) RecyclerView(android.support.v7.widget.RecyclerView) View(android.view.View) StartBrotherEvent(me.yokeyword.sample.demo_wechat.event.StartBrotherEvent) HomeAdapter(me.yokeyword.sample.demo_wechat.adapter.HomeAdapter) RecyclerView(android.support.v7.widget.RecyclerView)

Aggregations

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