点击 加 fragment

布局

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/btn_add_channel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:padding="10dp"
        android:src="@drawable/btn_add" />

    <android.support.design.widget.TabLayout
        android:id="@+id/tbl_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@id/btn_add_channel"></android.support.design.widget.TabLayout>
</RelativeLayout>

<android.support.v4.view.ViewPager
    android:id="@+id/vp_news"
    android:layout_width="match_parent"
    android:layout_height="match_parent"></android.support.v4.view.ViewPager>

MainActivity

package com.bwie.syx;

import android.content.Intent;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;

import com.bwie.syx.base.BaseActivity;
import com.bwie.syx.bean.Channel;
import com.bwie.syx.db.ChannelDao;
import com.bwie.syx.fragment.NewsFragment;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends BaseActivity implements View.OnClickListener {
private ImageView btnAddChannel;
private TabLayout tblTitle;
private ViewPager vpNews;

private List<Channel> channelList;
private ChannelDao dao;
private FragmentPagerAdapter vpAdapter;
public static final int REQUEST_CODE = 100;


@Override
protected int getContentView() {
    return R.layout.activity_main;
}

@Override
protected void initView() {
    btnAddChannel = findViewById(R.id.btn_add_channel);
    tblTitle = findViewById(R.id.tbl_title);
    vpNews = findViewById(R.id.vp_news);
}

@Override
protected void initData() {
    channelList = new ArrayList<>();
    dao = new ChannelDao(this);
    addData();

    vpAdapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
        @Override
        public Fragment getItem(int position) {
            return NewsFragment.newInstance(channelList.get(position).getParma());
        }

        @Override
        public int getCount() {
            return channelList.size();
        }

        @Nullable
        @Override
        public CharSequence getPageTitle(int position) {
            return channelList.get(position).getName();
        }
    };

    vpNews.setAdapter(vpAdapter);

    tblTitle.setTabMode(TabLayout.MODE_SCROLLABLE);
    tblTitle.setupWithViewPager(vpNews);
}

@Override
protected void onResume() {
    super.onResume();
    channelList.clear();
    List<Channel> channels = dao.queryAll();
    for (Channel channel : channels) {
        // 如果type为1,添加到我的频道中
        if (channel.getType() == 1) {
            channelList.add(channel);
        }
    }
    vpAdapter.notifyDataSetChanged();

}

@Override
protected void setListener() {
    super.setListener();
    btnAddChannel.setOnClickListener(this);
}

private void addData() {
    List<Channel> channels = dao.queryAll();
    for (Channel channel : channels) {
        // 如果type为1,添加到我的频道中
        if (channel.getType() == 1) {
            channelList.add(channel);
        }
    }
}


@Override
public void onClick(View v) {
    switch (v.getId()) {
        case R.id.btn_add_channel:
            Intent intent = new Intent(this, ChannelActivity.class);
            startActivityForResult(intent, REQUEST_CODE);
            break;
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) {
        int index = data.getIntExtra("index", 0);
        vpNews.setCurrentItem(index);
    }
}

}

猜你喜欢

转载自blog.csdn.net/chuanchuandaxia/article/details/82834144