<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<FrameLayout
android:id="@+id/left_view"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"/>
<View
android:layout_width="2dp"
android:layout_height="match_parent"
android:background="#9d9d9d"/>
<FrameLayout
android:id="@+id/right_view"
android:layout_width="0dp"
android:layout_weight="3"
android:layout_height="match_parent"/>
LinearLayout>
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
public class MainActivity extends FragmentActivity implements LeftFragment.ToRightFragment{
private FragmentManager manager;
private FragmentTransaction transaction;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
manager = getSupportFragmentManager();
transaction = manager.beginTransaction();
// 替换占位布局为自定义Fragment,tag值为fragment的标示符
transaction.replace(R.id.left_view, new LeftFragment(), "left");
transaction.replace(R.id.right_view, new RightFragment(), "right");
transaction.commit();
}
/**
* 接口回调方法(在类的声明中实现接口)
* @param index 实现接口方法时传的int值
*/
@Override
public void onToRightFragment(int index) {
// 通过Tag,找到对应的Fragment
RightFragment rightFragment = (RightFragment) manager.findFragmentByTag("right");
// 通过viewPager的get方法,获得RightFragment中的viewPager
ViewPager viewPager = rightFragment.getViewPager();
switch (index){
case 0:
viewPager.setCurrentItem(0);
break;
case 1:
viewPager.setCurrentItem(1);
break;
case 2:
viewPager.setCurrentItem(2);
break;
case 3:
viewPager.setCurrentItem(3);
break;
case 4:
viewPager.setCurrentItem(4);
break;
}
}
}
通过接口回调,设置ViewPager的当前页码,这样点击Button,ViewPager设置对应的页码,变相的实现了类似于跳转的功能
LeftFragment.class
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
public class LeftFragment extends Fragment implements View.OnClickListener {
private Button btn1, btn2, btn3, btn4, btn5;
// 定义自定义接口
private ToRightFragment listener;
/**
* 自定义接口
*/
public interface ToRightFragment {
// 接口中的方法,参数为int类型的值
void onToRightFragment(int index);
}
/**
* fragment与activity发生关联
* @param context
*/
@Override
public void onAttach(Context context) {
super.onAttach(context);
// 将context转型为接口 赋值给接口对象
listener = (ToRightFragment) context;
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_left, null);
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
initView(getView());
}
private void initView(View view) {
btn1 = (Button) view.findViewById(R.id.btn_1);
btn2 = (Button) view.findViewById(R.id.btn_2);
btn3 = (Button) view.findViewById(R.id.btn_3);
btn4 = (Button) view.findViewById(R.id.btn_4);
btn5 = (Button) view.findViewById(R.id.btn_5);
btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
btn3.setOnClickListener(this);
btn4.setOnClickListener(this);
btn5.setOnClickListener(this);
}
/**
* Button的监听事件
* 点击Button实现接口方法,传入一个int类型的值
*/
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_1:
listener.onToRightFragment(0);
break;
case R.id.btn_2:
listener.onToRightFragment(1);
break;
case R.id.btn_3:
listener.onToRightFragment(2);
break;
case R.id.btn_4:
listener.onToRightFragment(3);
break;
case R.id.btn_5:
listener.onToRightFragment(4);
break;
}
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/btn_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="国内歌手"/>
<Button
android:id="@+id/btn_2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="欧美歌手"/>
<Button
android:id="@+id/btn_3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="外星歌手"/>
<Button
android:id="@+id/btn_4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="太阳歌手"/>
<Button
android:id="@+id/btn_5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="银河歌手"/>
LinearLayout>
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import java.util.ArrayList;
public class RightFragment extends Fragment{
private ViewPager viewPager;
// 给viewPager一个get方法
public ViewPager getViewPager() {
return viewPager;
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_right, null);
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
viewPager = (ViewPager) getView().findViewById(R.id.view_pager);
ArrayList data = new ArrayList<>();
data.add(new InfoFragment("国内歌手"));
data.add(new InfoFragment("欧美歌手"));
data.add(new InfoFragment("外星歌手"));
data.add(new InfoFragment("太阳歌手"));
data.add(new InfoFragment("银河歌手"));
MyAdapter adapter = new MyAdapter(getActivity().getSupportFragmentManager(), data);
viewPager.setAdapter(adapter);
}
/**
* 适配器
*/
private class MyAdapter extends FragmentPagerAdapter {
private ArrayList lists;
public MyAdapter(FragmentManager fm, ArrayList lists) {
super(fm);
this.lists = lists;
}
@Override
public Fragment getItem(int position) {
return lists != null && lists.size() > 0 ? lists.get(position) : null;
}
@Override
public int getCount() {
return lists != null && lists.size() > 0 ? lists.size() : 0;
}
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.view.ViewPager
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
LinearLayout>
package com.wu.blogdemo;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class InfoFragment extends Fragment{
private String info;
public InfoFragment() {
}
public InfoFragment(String info) {
this.info = info;
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_info, null);
TextView tv = (TextView) v.findViewById(R.id.info_show);
tv.setText(info);
return v;
}
}
<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/info_show"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:textSize="24sp"/>
LinearLayout>