对recycleView的页面进行点击跳转设计

一.消息界面跳转聊天界面

1.建立MainActivity2以及其对应的layout文件实现跳转

如图所示:
在这里插入图片描述
在这里插入图片描述
其中xml文件对应的textview应该为“朋友”(为了跳转之后的界面便于理解)
在这里插入图片描述
JAVA文件所对应的代码如下:

package com.example.mywork;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.TextView;

public class MainActivity2 extends AppCompatActivity {

    private TextView textView21;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);  //取消默认title
        setContentView(R.layout.activity_main2);

        Intent intent=getIntent();

        textView21=findViewById(R.id.textView21);
        textView21.setText(intent.getStringExtra("num"));
        Log.d("life","activity2 is onCreate...");


    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.d("life", "activity2 is onStart...");
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        Log.d("life", "activity2 is onRestart...");
    }

    @Override
    protected void onPostResume() {
        super.onPostResume();
        Log.d("life", "activity2 is onPostResume...");
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.d("life", "activity2 is onPuse...");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.d("life", "activity2 is onStop...");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.d("life", "activity2 is onDestroy...");
    }

}

2.建立RecyItemTouchHelperCallback.java文件实现跳转

代码如下:

package com.example.mywork;

import android.content.Intent;
import android.graphics.Color;
import android.view.View;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.ItemTouchHelper;


import java.util.Collections;


public class RecyItemTouchHelperCallback extends ItemTouchHelper.Callback {
    RecyclerView.Adapter mAdapter;
    boolean isSwipeEnable;
    boolean isFirstDragUnable;

    public RecyItemTouchHelperCallback(RecyclerView.Adapter adapter) {
        mAdapter = adapter;
        isSwipeEnable = true;
        isFirstDragUnable = false;
    }

    public RecyItemTouchHelperCallback(RecyclerView.Adapter adapter, boolean isSwipeEnable, boolean isFirstDragUnable) {
        mAdapter = adapter;
        this.isSwipeEnable = isSwipeEnable;
        this.isFirstDragUnable = isFirstDragUnable;
    }


    @Override
    public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
        if (recyclerView.getLayoutManager() instanceof GridLayoutManager) {
            int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN |
                    ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT;
            int swipeFlags = 0;
            return makeMovementFlags(dragFlags, swipeFlags);
        } else {
            int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN;
            int swipeFlags = ItemTouchHelper.START | ItemTouchHelper.END;
            return makeMovementFlags(dragFlags, swipeFlags);
        }
    }

    @Override
    public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
        int fromPosition = viewHolder.getAdapterPosition();
        int toPosition = target.getAdapterPosition();
        if (isFirstDragUnable && toPosition == 0) {
            return false;
        }
        if (fromPosition < toPosition) {
            for (int i = fromPosition; i < toPosition; i++) {
                Collections.swap(((MyAdapter2) mAdapter).getDataList(), i, i + 1);
            }
        } else {
            for (int i = fromPosition; i > toPosition; i--) {
                Collections.swap(((MyAdapter2) mAdapter).getDataList(), i, i - 1);
            }
        }
        mAdapter.notifyItemMoved(fromPosition, toPosition);
        return true;
    }

    @Override
    public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
        int adapterPosition = viewHolder.getAdapterPosition();
        mAdapter.notifyItemRemoved(adapterPosition);
        ((MyAdapter2) mAdapter).getDataList().remove(adapterPosition);
    }

    @Override
    public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) {
        if (actionState != ItemTouchHelper.ACTION_STATE_IDLE) {
            viewHolder.itemView.setBackgroundColor(Color.LTGRAY);
        }
        super.onSelectedChanged(viewHolder, actionState);
    }

    @Override
    public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
        super.clearView(recyclerView, viewHolder);
        viewHolder.itemView.setBackgroundColor(Color.WHITE);
    }


    @Override
    public boolean isLongPressDragEnabled() {
        return !isFirstDragUnable;
    }

    @Override
    public boolean isItemViewSwipeEnabled() {
        return isSwipeEnable;
    }
}

同时,我是建立了两个适配器adapter来实现跳转的,如图:
在这里插入图片描述

3.将之前的message的JAVA文件按与xml文件进行布局修改

xml文件如图:
在这里插入图片描述
JAVA文件代码如下:

package com.example.mywork;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class messageFragment extends Fragment {

    private RecyclerView recyclerView2;
    private Context context;
    private MyAdapter2 myAdapter2;

    public messageFragment() {
        // Required empty public constructor
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        // 压缩器,把xml文件压缩到容器中
        View view=inflater.inflate(R.layout.fragment_message, container, false);
        context=this.getActivity();
        recyclerView2=view.findViewById(R.id.recyclerview2);

        String[] user = new String[20];
        String[] info = new String[20];
        for (int i=0;i<20;i++){
            user[i]=("朋友"+i);
            info[i]=("这是第"+i+"条消息");
        }

        List<Map<String,Object>> items2=new ArrayList<Map<String,Object>>();
        for(int i=0;i<user.length;i++) {
            Map<String,Object> item2=new HashMap<String,Object>();
            item2.put("USER", user[i]);
            item2.put("MESSAGE",info[i]);
            item2.put("png", R.drawable.user);
            items2.add(item2);
        }
        myAdapter2=new MyAdapter2(items2,context);
        recyclerView2.setAdapter(myAdapter2);
        LinearLayoutManager layoutmanager2 = new LinearLayoutManager(this.getActivity());
        layoutmanager2.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView2.setLayoutManager(layoutmanager2);

        myAdapter2.setOnItemClickListener(new MyAdapter2.OnItemClickListener(){
            @Override
            public void onItemClick(View view , int position){
                Intent intent=new Intent(context,MainActivity2.class);
                intent.putExtra("num",user[position]);
                startActivityForResult(intent,1);
            }
        });


        RecyItemTouchHelperCallback itemTouchHelperCallback = new RecyItemTouchHelperCallback(myAdapter2);
        final ItemTouchHelper itemTouchHelper = new ItemTouchHelper(itemTouchHelperCallback);
        itemTouchHelper.attachToRecyclerView(recyclerView2);
        // 添加分割线
        recyclerView2.addItemDecoration(new DividerItemDecoration(this.getActivity(), DividerItemDecoration.VERTICAL));

        return view;
    }

}


最后的结果如图:
在这里插入图片描述
在这里插入图片描述

要实现RecyclerView点击项目后跳转到详细页面展示,可以按照以下步骤进行: 1. 在RecyclerView的Adapter中,为每个项目添加点击事件监听器。 2. 在点击事件监听器中,获取被点击的项目的数据,比如可以通过getItem(position)方法获取对应位置的数据。 3. 将获取到的数据传递给详细页面展示的Activity,并启动该Activity。 4. 在详细页面展示的Activity中,接收传递过来的数据,并将其展示在界面上。 以下是示例代码: 在RecyclerView的Adapter中实现点击事件监听器: ```java public class MyAdapter extends RecyclerView.Adapter<MyViewHolder> { private List<Data> dataList; private Context context; public MyAdapter(List<Data> dataList, Context context) { this.dataList = dataList; this.context = context; } @Override public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = LayoutInflater.from(context).inflate(R.layout.item_layout, parent, false); MyViewHolder viewHolder = new MyViewHolder(view); return viewHolder; } @Override public void onBindViewHolder(MyViewHolder holder, final int position) { final Data data = dataList.get(position); holder.itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(context, DetailActivity.class); intent.putExtra("data", data); context.startActivity(intent); } }); // 设置数据 // ... } @Override public int getItemCount() { return dataList.size(); } } ``` 在详细页面展示的Activity中,接收传递过来的数据,并将其展示在界面上: ```java public class DetailActivity extends AppCompatActivity { private Data data; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_detail); // 获取传递过来的数据 Intent intent = getIntent(); data = (Data) intent.getSerializableExtra("data"); // 将数据展示在界面上 // ... } } ``` 需要注意的是,如果要传递的数据是自定义的类对象,那么该类必须实现Serializable接口,以便能够在Intent中传递。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值