Lolik

not404

nothing
x
bilibili
github
telegram
steam
follow

WeChat-like interface 02

1. Function Description#

Edit Function
psLxi

Delete Function
psD8L

View Detail Page
ps4DC

2. Core Code#

XML Part#

item.xml
psV1Y

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/linearLayout_item1"
    android:layout_width="match_parent"
    android:layout_height="80dp"
    android:background="@drawable/underline_list"
    android:orientation="horizontal">

    <ImageView
        android:id="@+id/imageView_item1"
        android:layout_width="72dp"
        android:layout_height="53dp"
        android:layout_gravity="center"
        app:srcCompat="@drawable/ic___wx_this" />

    <TextView
        android:id="@+id/textView_item1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:text="Number"
        android:textSize="16dp" />

    <TextView
        android:id="@+id/textView_item2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"

        android:text="Content"
        android:textSize="16dp" />

</LinearLayout>

tab02.xml

ps9xc

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:background="@color/hui"
    android:layout_height="match_parent"
    android:orientation="horizontal">


    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:overScrollMode="never"
        android:scrollbars="none"
        android:visibility="visible"

        />

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:layout_gravity="end|bottom"
        android:layout_marginStart="16dp"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="36dp"
        android:layout_marginBottom="41dp"
        android:contentDescription="add"
        android:src="@drawable/ic_action_name" />

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/constraintlayout_none"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:layout_marginStart="2dp"
        android:layout_marginTop="48dp"
        android:layout_marginEnd="-2dp"
        android:layout_marginBottom="461dp"
        android:background="@color/hui"
        android:visibility="gone">

        <EditText
            android:id="@+id/text_add"
            android:layout_width="294dp"
            android:layout_height="58dp"
            android:layout_marginTop="4dp"
            android:ems="10"
            android:hint="Please enter..."
            android:imeOptions="actionNext"
            android:inputType="text"
            android:minHeight="48dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <Button
            android:id="@+id/button_cl"
            android:layout_width="117dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="36dp"
            android:text="Cancel"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toStartOf="@+id/button_ok"
            app:layout_constraintHorizontal_bias="0.444"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/text_add"
            app:layout_constraintVertical_bias="0.0" />

        <Button
            android:id="@+id/button_ok"
            android:layout_width="137dp"
            android:layout_height="48dp"
            android:layout_marginTop="36dp"
            android:layout_marginEnd="32dp"
            android:text="Confirm"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/text_add"
            app:layout_constraintVertical_bias="0.0" />
    </androidx.constraintlayout.widget.ConstraintLayout>

</RelativeLayout>

The input box starts in a hidden state

Java Part#

Set the content of three components in onBindViewHolder

        holder.textView1.setText("" + position);//Convert to string
        holder.textView2.setText(list.get(position));
        holder.imageView_item1.setImageResource(addimg(position));

Listener in onBindViewHolder

holder.linearLayout_item1.setOnTouchListener(new View.OnTouchListener() {
            private int X;
            private int lastX;
            private int Y;
            @SuppressLint("ResourceAsColor")
            @Override
            public boolean onTouch(View view, MotionEvent event) {
                switch (event.getAction() & MotionEvent.ACTION_MASK) {
                    case MotionEvent.ACTION_DOWN://Triggered when the first touch point is detected on the screen.
                        lastX = (int) event.getRawX();
                        Y = (int) event.getRawY();
                        X = (int) event.getRawX();
                        break;
                    case MotionEvent.ACTION_UP://Triggered when the touch point is released.
                        if (Math.abs((int) event.getRawX() - X)<2){//Single click, no change in click coordinates
                            Intent intent = new Intent(holder.itemView.getContext(), MainActivity2.class);
                            intent.putExtra("msg","position:"+position+"\n"+list.get(position));
                            context.startActivity(intent);
                        }
                        if(Math.abs(((int) event.getRawX() - X))<200){
                            holder.linearLayout_item1.setLeft(0);
                        }else{
                            onSwiped(position);
                            holder.linearLayout_item1.setLeft(0);
                        }
                        break;
                    case MotionEvent.ACTION_MOVE:
                        int dx = (int) event.getRawX() - lastX;
                        int left = view.getLeft() + dx;
                        holder.linearLayout_item1.setLeft(left);
                        lastX = (int) event.getRawX();
                        break;
                }
                return true;
            }
        });

Several triggered events of MotionEvent, currently there is a bug. The judgment of vertical scrolling and horizontal sliding
Judgment of dragging or clicking: based on the difference in coordinates between touch and release within a certain range

Delete Data

//  Delete data
    @SuppressLint("NotifyDataSetChanged")
    public void onSwiped(int p) {
        list.remove(p);
        notifyItemRemoved(p);
        notifyDataSetChanged();
    }

In Fragment.java, Viewinit() function

private void Viewinit() {
        context = this.getActivity();
        //fullview=getView();
        myadapter = new Adapter(context);
        //Set LayoutManager
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setHasFixedSize(true);
        recyclerView.setAdapter(myadapter);
        myadapter.list(list);

    }

Adding to the list

@SuppressLint("NotifyDataSetChanged")
    @Override
    public void onStart() {
        super.onStart();
        fab=getActivity().findViewById(R.id.fab);
        ConstraintLayout constraintlayout_none=getActivity().findViewById(R.id.constraintlayout_none);
        EditText text_add=getActivity().findViewById(R.id.text_add);
        Button button_cl=getActivity().findViewById(R.id.button_cl);
        Button button_ok=getActivity().findViewById(R.id.button_ok);
        InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);   ;
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                constraintlayout_none.setVisibility(View.VISIBLE);
                recyclerView.setVisibility(View.INVISIBLE);
                imm.showSoftInput(text_add, 0);
                text_add.setText("");
                button_ok.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        String s=text_add.getText().toString();
                        constraintlayout_none.setVisibility(View.GONE);
                        list.add(s);
                        Log.d("add","+1");
                        imm.hideSoftInputFromWindow(text_add.getWindowToken(), 0);
                        myadapter.notifyDataSetChanged();
                        recyclerView.scrollToPosition(myadapter.getItemCount() - 1);
                        Toast.makeText(context, "Added successfully", Toast.LENGTH_SHORT).show();
                        recyclerView.setVisibility(View.VISIBLE);
                    }
                });
                button_cl.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        constraintlayout_none.setVisibility(View.GONE);
                        Toast.makeText(context, "Add canceled", Toast.LENGTH_SHORT).show();
                        recyclerView.setVisibility(View.VISIBLE);
                        imm.hideSoftInputFromWindow(text_add.getWindowToken(), 0);
                    }
                });
                //list.add("111111");
                //Log.d("add",list.toString());


            }
        });


    }

The idea is to show the input layout after clicking the add button, hide the RecyclerView, and open the keyboard.

  • If you click Confirm, the input value will be added to the list, refresh the RecyclerView, hide the input layout, close the keyboard, and show the RecyclerView.

  • If you click Cancel, directly restore the view (i.e., hide the input layout, show the RecyclerView...)

Complete Code#


Adapter.java

package com.example.applicationtest;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.List;

public class Adapter extends RecyclerView.Adapter<Adapter.Myholder> {
    private View itemview;
    private Context context;

    private List<String> list;


    public int addimg(int i) {
        if (i % 4 == 0) {
            return R.drawable.num_a;
        } else if (i % 4 - 1 == 0) {
            return R.drawable.num_b;
        } else if (i % 4 - 2 == 0) {
            return R.drawable.num_c;
        } else if (i % 4 - 3 == 0) {
            return R.drawable.num_d;
        } else {
            return R.drawable.ic___fx;
        }
    }
    public Adapter(Context context) {

        this.context = context;
    }

    public void list(List<String> list) {
        this.list = list;
        notifyDataSetChanged();

    }
    
    @NonNull
    @Override
    public Myholder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        itemview = LayoutInflater.from(context).inflate(R.layout.item1, parent, false);
        Myholder holder;

        holder = new Myholder(itemview);
        return holder;
    }

    @SuppressLint({"SetTextI18n", "ClickableViewAccessibility"})
    @Override
    public void onBindViewHolder(@NonNull Myholder holder, @SuppressLint("RecyclerView") int position) {
        holder.textView1.setText("" + position);//Convert to string
        holder.textView2.setText(list.get(position));
        holder.imageView_item1.setImageResource(addimg(position));
//        holder.linearLayout_item1.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                //Toast.makeText(itemview.getContext(), position + "号:" + list.get(position), Toast.LENGTH_SHORT).show();
//                Intent intent = new Intent(holder.itemView.getContext(), MainActivity2.class);
//                intent.putExtra("msg","position:"+position+"\n"+list.get(position));
//                context.startActivity(intent);
//                //onSwiped(position);
//            }
//        });
        holder.linearLayout_item1.setOnTouchListener(new View.OnTouchListener() {
            private int X;
            private int lastX;
            private int Y;
            @SuppressLint("ResourceAsColor")
            @Override
            public boolean onTouch(View view, MotionEvent event) {
                switch (event.getAction() & MotionEvent.ACTION_MASK) {
                    case MotionEvent.ACTION_DOWN://Triggered when the first touch point is detected on the screen.
                        lastX = (int) event.getRawX();
                        Y = (int) event.getRawY();
                        X = (int) event.getRawX();
                        break;
                    case MotionEvent.ACTION_UP://Triggered when the touch point is released.
                        if (Math.abs((int) event.getRawX() - X)<2){//Single click, no change in click coordinates
                            Intent intent = new Intent(holder.itemView.getContext(), MainActivity2.class);
                            intent.putExtra("msg","position:"+position+"\n"+list.get(position));
                            context.startActivity(intent);
                        }
                        if(Math.abs(((int) event.getRawX() - X))<200){
                            holder.linearLayout_item1.setLeft(0);
                        }else{
                            onSwiped(position);
                            holder.linearLayout_item1.setLeft(0);
                        }
                        break;
                    case MotionEvent.ACTION_MOVE:
                        int dx = (int) event.getRawX() - lastX;
                        int left = view.getLeft() + dx;
                        holder.linearLayout_item1.setLeft(left);
                        lastX = (int) event.getRawX();
                        break;
                }
                return true;
            }
        });
    }
    //  Delete data
    @SuppressLint("NotifyDataSetChanged")
    public void onSwiped(int p) {
        list.remove(p);
        notifyItemRemoved(p);
        notifyDataSetChanged();
    }


    @Override
    public int getItemCount() {
        return list.size();
    }

    public static class Myholder extends RecyclerView.ViewHolder {

        private TextView textView1, textView2;
        private ImageView imageView_item1;
        private LinearLayout linearLayout_item1;

        public Myholder(@NonNull View itemView) {
            super(itemView);
            textView1 = itemView.findViewById(R.id.textView_item1);
            textView2 = itemView.findViewById(R.id.textView_item2);
            imageView_item1 = itemView.findViewById(R.id.imageView_item1);
            linearLayout_item1 = itemView.findViewById(R.id.linearLayout_item1);


        }
    }

}

Fragment2.java

package com.example.applicationtest;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.InputType;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.android.material.floatingactionbutton.FloatingActionButton;

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

import javax.security.auth.PrivateCredentialPermission;


public class Fragment2 extends Fragment {

    private RecyclerView recyclerView;
    private Context context;
    private FloatingActionButton fab;
    private Adapter myadapter;
    private List<String> list = new ArrayList<>();


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view;
        view = inflater.inflate(R.layout.tab02, container, false);

        recyclerView = view.findViewById(R.id.recyclerView1);
        listdata();
        Viewinit();
        return view;



    }

    @SuppressLint("NotifyDataSetChanged")
    @Override
    public void onStart() {
        super.onStart();
        fab=getActivity().findViewById(R.id.fab);
        ConstraintLayout constraintlayout_none=getActivity().findViewById(R.id.constraintlayout_none);
        EditText text_add=getActivity().findViewById(R.id.text_add);
        Button button_cl=getActivity().findViewById(R.id.button_cl);
        Button button_ok=getActivity().findViewById(R.id.button_ok);
        InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);   ;
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                constraintlayout_none.setVisibility(View.VISIBLE);
                recyclerView.setVisibility(View.INVISIBLE);
                imm.showSoftInput(text_add, 0);
                text_add.setText("");
                button_ok.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        String s=text_add.getText().toString();
                        constraintlayout_none.setVisibility(View.GONE);
                        list.add(s);
                        Log.d("add","+1");
                        imm.hideSoftInputFromWindow(text_add.getWindowToken(), 0);
                        myadapter.notifyDataSetChanged();
                        recyclerView.scrollToPosition(myadapter.getItemCount() - 1);
                        Toast.makeText(context, "Added successfully", Toast.LENGTH_SHORT).show();
                        recyclerView.setVisibility(View.VISIBLE);
                    }
                });
                button_cl.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        constraintlayout_none.setVisibility(View.GONE);
                        Toast.makeText(context, "Add canceled", Toast.LENGTH_SHORT).show();
                        recyclerView.setVisibility(View.VISIBLE);
                        imm.hideSoftInputFromWindow(text_add.getWindowToken(), 0);
                    }
                });
                //list.add("111111");
                //Log.d("add",list.toString());


            }
        });


    }

    private void Viewinit() {
        context = this.getActivity();
        //fullview=getView();
        myadapter = new Adapter(context);
        //Set LayoutManager
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setHasFixedSize(true);
        recyclerView.setAdapter(myadapter);
        myadapter.list(list);

    }

    private void listdata() {
        list.add("Bright sunshine and bright girls······ do they match well?");
        list.add("Don't move, let me borrow your eyes to look in the mirror. Do you feel nostalgic?");
        list.add("Are my eyes beautiful? This is not colored contact lenses, it's the magic of a beautiful girl.");
        ...

    }


}

3. Experiment Summary#

  • Inherit from RecyclerView.ViewHolder and write your own ViewHolder.

  • Implement onCreateViewHolder(ViewGroup parent, int viewType)
    This method will be called when the RecyclerView needs us to provide a new ViewHolder of type viewType. Instantiate the root layout of Item and return a ViewHolder bound to it.

  • Implement onBindViewHolder(VH viewHolder, int position)
    This method will be called when the RecyclerView needs to display the data corresponding to the position. Update the view through the View held by the viewHolder at the corresponding position.

  • Implement getItemCount()
    Return the total number of Items.

Code Repository:#

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.