在FrameLayout上放置3个fragment:channelListFragment, programListFragment, commentListFragment. 要求利用手势左右滑动programListFragment。

实现简单介绍:调用programListFragment.getView().setOnTouchListener(listener)来设置onTouch事件。在onTouch方法中通过动态修改leftMargin属性来实现滑动效果。

WatchTvFragment.java

public class WatchTvFragment extends Fragment {
                                                                                                                    
    private static View view;
    String TAG = "WatchTvFragment";
                                                                                                                    
    TvChannelListFragment channelListFragment;
    TvProgramListFragment programListFragment;
    TvCommentListFragment commentListFragment;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (view != null) {
            ViewGroup parent = (ViewGroup) view.getParent();
            if (parent != null)
                parent.removeView(view);
        }
        try {
            view = inflater.inflate(R.layout.fragment_watchtv, container, false);
        } catch (InflateException e) {
            /* fragment_main is already there, just return view as it is */
        }
        channelListFragment = (TvChannelListFragment) getFragmentManager().findFragmentById(R.id.channel_list_fragment);
        programListFragment = (TvProgramListFragment) getFragmentManager().findFragmentById(R.id.program_list_fragment);
        commentListFragment = (TvCommentListFragment) getFragmentManager().findFragmentById(R.id.comment_list_fragment);
        return view;
    } 
                                                                                                                    
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        getView().setOnTouchListener(watchTvTouchListener);
        programListFragment.getView().setOnTouchListener(programTouchListener);
    }
                                                                                                                    
    View.OnTouchListener watchTvTouchListener = new View.OnTouchListener() {
                                                                                                                        
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // Logger.i(TAG, "++watchTvTouchListener.onTouch++");
            return false;
        }
    };
                                                                                                                    
    View.OnTouchListener programTouchListener = new View.OnTouchListener() {
                                                                                                                        
        float xDown;
        float xLast;
        float xCurrent;
        float xUp;
        float xDistance;
        FrameLayout.LayoutParams params = null;
                                                                                                                        
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // Logger.i(TAG, "++programTouchListener.onTouch++");
            switch(event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                // Logger.i(TAG, "event.getAction():"+MotionEvent.ACTION_DOWN);
                xDown = event.getX();
                xCurrent = xDown;
                break;
            case MotionEvent.ACTION_MOVE:
                xLast = xCurrent;
                xCurrent = event.getX();
                xDistance = xCurrent - xLast;
                params = (FrameLayout.LayoutParams)programListFragment.getView().getLayoutParams();
                // 打印坐标日志
                // Logger.i(TAG, "event.getAction():"+MotionEvent.ACTION_MOVE);
                // Logger.i(TAG, "xLast:"+xLast);
                // Logger.i(TAG, "xCurrent:"+xCurrent);
                // Logger.i(TAG, "xDistance:"+xDistance);
                // 在xml文件中使用leftMargin,在代码中也要使用leftMargin属性。
                // Logger.i(TAG, "1.params.getMarginStart():"+params.getMarginStart());
                // params.setMarginStart(params.getMarginStart() + (int)xDistance);
                params.leftMargin += (int)xDistance;
                programListFragment.getView().setLayoutParams(params);
                // 此时没必要调用invalidate()方法。
                // programListFragment.getView().invalidate();
                // getView().invalidate();
                // Logger.i(TAG, "2.params.getMarginStart():"+params.getMarginStart());
                break;
            case MotionEvent.ACTION_UP:
                // Logger.i(TAG, "event.getAction():"+MotionEvent.ACTION_UP);
                xUp = event.getX();
                xCurrent = xUp;
                break;
            }
            // 返回true,就不会再执行父View的onTouch事件(watchTvTouchListener)。
            return true;
        }
    };
}

fragment_watchtv.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
       
    <fragment
        android:id="@+id/channel_list_fragment"
        android:layout_width="400dp"
        android:layout_height="match_parent"
        class="com.tvie.ivideo.pad.live.TvChannelListFragment"/>
       
    <fragment
        android:id="@+id/program_list_fragment"
        android:layout_width="400dp"
        android:layout_height="match_parent"
        android:layout_marginLeft="200dp"
        class="com.tvie.ivideo.pad.live.TvProgramListFragment"/>
       
    <fragment
        android:id="@+id/comment_list_fragment"
        android:layout_width="400dp"
        android:layout_height="match_parent"
        android:layout_marginLeft="400dp"
        class="com.tvie.ivideo.pad.live.TvCommentListFragment"/>
       
</FrameLayout>