1 功能说明
??????? 在上次微信界面设计的基础上,对有recycleView的页面进行点击跳转设计; 比如,某一tab页是联系人列表,则点击某一行能跳转到联系人详情页面。并且设置一个返回按钮可以返回跳转。
2 跳转方法
我使用通过class跳转的方法
try {
Intent intent = new Intent();
//(当前Activity,目标Activity)
intent.setClass(MainActivity.this, Class.forName("com.cx.testdemo.TestActivity"));
startActivity(intent);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
?除此之外还有通过Activity的name跳转
Intent intent = new Intent();
//(当前Activity,目标Activity)
intent.setClass(MainActivity.this, TestActivity.class);
startActivity(intent);
为Activity设置Action,通过Action跳转
Intent intent = new Intent();
//(声明中的Action)
intent.setAction("");
//(声明中的Category)
intent.addCategory("");
startActivity(intent);
上面的Action和Category应该与Activity声明中设置的Action和Category相同
<activity android:name="">
<!-- intent-filter:过滤器一 -->
<intent-filter >
<!-- action随便设置,但必须保证唯一 -->
<action android:name=""/>
<category android:name=""/>
</intent-filter>
</activity>
3具体代码
MailActivitiy
package com.example.mywork;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class MailActivity extends AppCompatActivity {
//好友名字
private TextView friend_name;
//好友信息
private TextView friend_info;
//好友照片
private ImageView friend_pic;
//返回上个页面按钮
private Button btn_back;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.friend2);
init();
setListeners();
}
public void init() {
friend_name = (TextView) findViewById(R.id.friend_name);
friend_info = (TextView) findViewById(R.id.friend_info);
friend_pic = (ImageView) findViewById(R.id.friend_pic);
btn_back = (Button) findViewById(R.id.btn_back);
}
private void setListeners() {
btn_back.setOnClickListener(backMain);
}
private Button.OnClickListener backMain = new Button.OnClickListener() {
@Override
public void onClick(View arg0) {
MailActivity.this.finish();
}
};
}
布局配置文件friend2
package com.example.mywork;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class MailActivity extends AppCompatActivity {
//好友名字
private TextView friend_name;
//好友信息
private TextView friend_info;
//好友照片
private ImageView friend_pic;
//返回上个页面按钮
private Button btn_back;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.friend2);
init();
setListeners();
}
public void init() {
friend_name = (TextView) findViewById(R.id.friend_name);
friend_info = (TextView) findViewById(R.id.friend_info);
friend_pic = (ImageView) findViewById(R.id.friend_pic);
btn_back = (Button) findViewById(R.id.btn_back);
}
private void setListeners() {
btn_back.setOnClickListener(backMain);
}
private Button.OnClickListener backMain = new Button.OnClickListener() {
@Override
public void onClick(View arg0) {
MailActivity.this.finish();
}
};
}
?修改AndroidManifest.xml
添加
<activity
android:name=".MailActivity"
>
</activity>
修改CollectRecycleAdapter
@Override
public void onBindViewHolder(myViewHodler holder, int position) {
//根据点击位置绑定数据
final GoodsEntity data = goodsEntityList.get(position);
// holder.mItemGoodsImg;
holder.mItemGoodsName.setText(data.goodsName);//获取实体类中的name字段并设置
holder.mItemGoodsPrice.setText(data.goodsPrice);//获取实体类中的price字段并设置
//实现点击功能
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context, "您点击的联系人是:" + data.goodsName, Toast.LENGTH_SHORT).show();
Intent intent = new Intent();
intent.setClass(context,MailActivity.class);
context.startActivity(intent);
}
});
}
添加跳转功能
4结果展示
?
代码已上传至gitee仓库
墨黑色的白/Android
?
?
?
?
?
|