在RecycleView的界面上实现跳转点击
实现效果
关于recycleView功能的大致介绍
我实现的方法是在某一tab界面出现recycleView列表,可实现上下拖动以及较为复杂的展开与收缩。
item.xml:
因为是展开收缩功能,所以设置了主副布局。
<RelativeLayout
android:id="@+id/rl_parent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#F3AD7F"
android:gravity="center">
<TextView
android:id="@+id/tv_team"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:text="主布局"
android:textColor="@android:color/black"
android:textSize="24sp"
android:textStyle="bold" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/rl_child"
android:layout_width="match_parent"
android:layout_height="80dp"
android:background="#E68566"
android:visibility="gone">
<TextView
android:id="@+id/tv_team_child"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="副布局"
android:textColor="#6C3737"
android:textSize="26sp" />
</RelativeLayout>
zhenzhu_fragment_wechat.xml:
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rcv_expandcollapse"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:overScrollMode="never"
android:scrollbars="none"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
并且修改了相应的Fragment文件,增加了适配器adapter,适配器中调用item.xml。
adapter的几个主要函数如下:
Fragment文件:
initData: 添加列表字段
initView: 与适配器绑定,实现列表相关效果
(相应代码在结尾处的码云地址中可以找到哦)
Activity实现跳转
为实现关于activity的生命周期以及状态转变操作。
跳转代码实现
首先建立activity详情界面,并且检查是否可以实现:
AndroidManifest.xml:
(能够看到我们的MainActivity2已经被声明,可以使用)
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.MyApplication">
<activity
android:name=".MainActivity3"
android:exported="true" />
<activity
android:name=".MainActivity2"
android:exported="true" />
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
接下来我们应当为activity详情界面以及XML文件完成相关配置,实现跳转界面的展示。
MainActivity2.java:
(通过在每个函数中加入Log,我们可以由此查看到activity的生命进程)
public class MainActivity2 extends AppCompatActivity {
//onCreate:第一个被调用的方法,进行activity的初始化
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Intent intent=getIntent();
Log.d("life","Activity2 is onCreate...");
}
//当activity显示在屏幕上时,该方法被调用
@Override
protected void onStart() {
Log.d("life","Activity2 is Start");
super.onStart();
}
//当activity对用户不可见,被调用进入停止状态
@Override
protected void onStop() {
Log.d("life","Activity2 is Stop");
super.onStop();
}
//当activity被终止前调用
@Override
protected void onDestroy() {
Log.d("life","Activity2 is Destroy");
super.onDestroy();
}
}
activity_main.xml:
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
tools:context=".MainActivity2">
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="333dp"
android:layout_weight="1"
android:text="All the puddings have been sold out !"
android:textAlignment="center"
android:textSize="48sp"
android:textStyle="italic"
android:typeface="serif" />
</LinearLayout>
实现点击跳转效果: 在适配器中添加相应的跳转代码。
因为我想要实现的功能如下:原来的点击收缩与展开不变,在此基础上,点击副布局列表的textview,可以跳转到我之前建立的MainActivity2界面上。由此我需要为tvTeamChild添加点击跳转效果:
viewHolder.tvTeamChild.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(v.getContext(),
"你点击了"+viewHolder.tvTeamChild.getText(),Toast.LENGTH_LONG).show();
Intent intent=new Intent(v.getContext(),MainActivity2.class);
v.getContext().startActivity(intent);
}
});
以上即可实现跳转。
查看Activity的生命周期
在我点击副布局列表时:
在我关闭跳转界面,返回主界面时:
由此我们可以知道:运行一个Andriod程序时,首先运行onCreate方法实现activity的初始化;当activity显示在屏幕上时,onStart方法被调用;当activity对用户不可见,onStop方法进入停止状态;当activity被终止前,onDestroy方法被调用。