1. 程式人生 > >事件分發,以及消除子控制元件和父控制元件互相的影響

事件分發,以及消除子控制元件和父控制元件互相的影響

效果圖 消除在上下滑的時候讓下面的listview控制元件不進行滑動 在這裡插入圖片描述

如題,先上xml的程式碼 父控制元件listview

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <ListView
        android:id="@+id/listview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"></ListView>

</android.support.constraint.ConstraintLayout>

父控制元件ListView的虛擬內容

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:id="@+id/listview_Name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="呵呵呵"></TextView>

</LinearLayout>

//圖片的程式碼

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@mipmap/ic_launcher_round" />

</LinearLayout>

優化後的 自己寫的控制元件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <com.bwie.administrator.shijian.eliminate.MyViewPage
        android:id="@+id/MyHe"
        android:layout_width="match_parent"
        android:layout_height="200dp"></com.bwie.administrator.shijian.eliminate.MyViewPage>
</LinearLayout>

父控制元件ListView的介面卡

package com.bwie.administrator.shijian.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.bwie.administrator.shijian.R;

import org.w3c.dom.Text;

import java.util.ArrayList;

public class MyAdapter extends BaseAdapter {
    private Context context;
    private ArrayList<String> datas;

    public MyAdapter(Context context, ArrayList<String> datas) {
        this.context = context;
        this.datas = datas;
    }

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

    @Override
    public Object getItem(int position) {
        return datas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        //宣告一個holder類
        ViewHolder holder = null;
        //進行判斷
        if (convertView == null){
            convertView = View.inflate(context,R.layout.listview_item,null);
            holder = new ViewHolder();
            holder.tv = convertView.findViewById(R.id.listview_Name);
            convertView.setTag(holder);
        }else{
            holder = (ViewHolder) convertView.getTag();
        }
        holder.tv.setText(datas.get(position));

        return convertView;
    }

    //holder類
    class ViewHolder{
        TextView tv;
    }

}

優化ViewPage的介面卡

package com.bwie.administrator.shijian.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;

public class MyPageAdapter extends PagerAdapter {
    private ArrayList<View> lists;

    public MyPageAdapter(ArrayList<View> lists) {
        this.lists = lists;
    }

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

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object o) {
        return view == o;
    }

    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {
        container.addView(lists.get(position));
        return lists.get(position);
    }

    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
        //刪除條目檢視
        container.removeView(lists.get(position));

    }
}

//事件分發消除viewpage和listview之間的影響

package com.bwie.administrator.shijian.eliminate;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.ViewGroup;

/**
 * parent.requestDisallowInterceptTouchEvent(true);
 * 當我觸控到子控制元件的時候,父控制元件不會影響你
 */
public class MyViewPage extends ViewPager {
    private ViewGroup parent;

    public MyViewPage(@NonNull Context context) {
        super(context);
    }

    public MyViewPage(Context context, AttributeSet attr) {
        super(context, attr);
    }

    public void setParent(ViewGroup parent) {
        this.parent = parent;
    }

    //


    @Override//事件分發
    public boolean dispatchTouchEvent(MotionEvent ev) {
        //進行判斷
        if (parent != null) {
            //請求不允許攔截觸控事件
            parent.requestDisallowInterceptTouchEvent(true);
        }
        return super.dispatchTouchEvent(ev);
    }

    @Override//攔截事件
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        //進行判斷
        if (parent != null) {
            parent.requestDisallowInterceptTouchEvent(true);
        }
        return super.onInterceptTouchEvent(ev);
    }
    @Override//消費 處理
    public boolean onTouchEvent(MotionEvent ev) {
        //進行判斷
        if (parent != null) {
            parent.requestDisallowInterceptTouchEvent(true);
        }
        return super.onTouchEvent(ev);
    }
}

MainActivity裡面的程式碼

package com.bwie.administrator.shijian;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.bwie.administrator.shijian.adapter.MyAdapter;
import com.bwie.administrator.shijian.adapter.MyPageAdapter;
import com.bwie.administrator.shijian.eliminate.MyViewPage;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    private ListView listview;
    private MyAdapter adapter;
    private View myHeaderView;
    private MyViewPage myViewPage;
    private ArrayList<String> datas = new ArrayList<>();
    private ArrayList<View> mPageListList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //初始化資料
        initData();
        //初始化檢視
        initHolderView();
        //查詢控制元件
        initView();
        //設定介面卡
        adapter = new MyAdapter(MainActivity.this, datas);
        listview.setAdapter(adapter);
    }

    private void initHolderView() {
        myHeaderView = getLayoutInflater().inflate(R.layout.holder_view, null);
        myViewPage = myHeaderView.findViewById(R.id.MyHe);
        //進行迴圈
        for (int i = 0; i < 10; i++) {
            View view = getLayoutInflater().inflate(R.layout.page_view, null);
            mPageListList.add(view);
        }
        myViewPage.setParent((ViewGroup) myViewPage.getParent());
        //設定介面卡
        myViewPage.setAdapter(new MyPageAdapter(mPageListList));

    }

    //初始化資料
    private void initData() {
        for (int i = 0; i < 50; i++) {
            datas.add("BigFlyRed" + i);
        }
    }

    //查詢控制元件
    private void initView() {
        listview = (ListView) findViewById(R.id.listview);
        listview.addHeaderView(myHeaderView);
    }
}

以上;