android solves the conflict between NestedScrollView and ListView

When you are doing development, many people need to use sliding scrollview, but there will be a conflict between the event of jam and sliding

Note to add dependency in the builder file under app, otherwise the control cannot be used

compile 'com.android.support.constraint:constraint-layout:1.0.2'

This blog implements a common mobile phone effect. The picture and listview display are as follows:

(1) New item for custom listview effect

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

    <ImageView
        android:id="@+id/iv"
        android:layout_width="80dp"
        android:layout_height="90dp"
        android:layout_centerVertical="true"
        android:padding="5dp"
        android:scaleType="centerCrop"
        android:src="@mipmap/e" />

    <TextView
        android:id="@+id/title"
        android:layout_marginLeft="10dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:layout_toRightOf="@id/iv"
        android:gravity="center_vertical"
        android:text="Ministry of human resources and social security: the first draft of pension transfer"
        android:textSize="14sp"
        android:textStyle="bold" />


    <EditText
        android:id="@+id/summary"
        android:layout_below="@id/title"
        android:layout_marginTop="8dp"
        android:layout_toRightOf="@id/iv"
        android:layout_width="220dp"
        android:layout_height="40dp"
        android:layout_marginLeft="10dp"
        android:hint="Please enter quantity"
        android:inputType="textCapCharacters"
        android:singleLine="true"
        android:maxLength="13"
        android:textSize="15sp"/>


</RelativeLayout>

(2) In activity main, write our layout effects, including listivw and pictures

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:src="@mipmap/a"
            android:scaleType="fitXY"
            />

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:src="@mipmap/b"
            android:scaleType="fitXY"
            />



        <ImageView
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:src="@mipmap/c"
            android:scaleType="fitXY"
            />

        <ListView
            android:id="@+id/lisview"
            android:layout_width="match_parent"
            android:layout_height="100dp"/>

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:src="@mipmap/d"
            android:scaleType="fitXY"
            />
    </LinearLayout>
</android.support.v4.widget.NestedScrollView>

(3) If you simply load the layout and configuration adapter in MainActivity code, listview will not display completely

import android.content.Context;
        import android.support.v7.app.AppCompatActivity;
        import android.os.Bundle;
        import android.support.v7.widget.AppCompatTextView;
        import android.view.LayoutInflater;
        import android.view.View;
        import android.view.ViewGroup;
        import android.widget.BaseAdapter;
        import android.widget.ListAdapter;
        import android.widget.ListView;
        import android.widget.TextView;

/**
 Problem Description:
 1)  What happens when a listview is nested directly in android.support.v4.widget.NestedScrollView: listview displays not only one or two lines.
 2) If the item of listview has the option to select based on id, the selection will be disordered. If the item of listview contains the switch control,
 It is used to judge whether the item is available or not, but now the position in listview caused by the conflict is disordered.
 2.Solution: calculate the height of each item. This method is used after listview.setAdapter().
 */

public class MainActivity extends AppCompatActivity {
    //Reference website: https://www.cnblogs.com/yuzhongzheng/p/5228401.html
    private MyAdapter adapter;
    private int totalHeight = 0;  //Define total height


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ListView listview = (ListView) findViewById(R.id.lisview);
        adapter = new MyAdapter(this);
        if (adapter==null){
            return;
        }
        //According to listAdapter.getCount(), get how many item items are currently owned,
        // Then traverse to get the height of each item and add to get the total height.
        for (int i=0;i<adapter.getCount();i++){
            View listitem = adapter.getView(i,null,listview);
            listitem.measure(0,0);
            totalHeight += listitem.getMeasuredHeight();
        }
        //Get layout properties of list
        ViewGroup.LayoutParams params = listview.getLayoutParams();
        //The final height of listview is the height of item + the height of separator. This is to reset the property of listview
        params.height = totalHeight + (listview.getDividerHeight()*(adapter.getCount()-1));
        //Apply the reset params to listview
        listview.setLayoutParams(params);
        listview.setAdapter(adapter);
    }
}

class MyAdapter extends BaseAdapter {
    Context context;

    public MyAdapter(Context context) {
        super();
        this.context = context;
    }


    @Override
    public int getCount() {
        return 10;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = LayoutInflater.from(context).inflate(R.layout.item,null);
        return view;
    }

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

    @Override
    public Object getItem(int position) {
        return null;
    }
}

 

Tags: Android xml encoding Mobile

Posted on Mon, 06 Jan 2020 10:49:37 -0800 by Zack