一、实现效果
1.列表展示+背景色随机
2.下拉刷新(待完善)
3.滑动删除(待完善)
二、代码展示
//mainActivity 继(二)学习记录
package com.e.spirit;
//import androidx.appcompat.app.AppCompatActivity;
//import androidx.fragment.app.Fragment;
//import androidx.fragment.app.FragmentManager;
//import androidx.fragment.app.FragmentTransaction;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;
//import androidx.fragment.app.Fragment;
public class MainActivity extends Activity implements View.OnClickListener{
private Fragment todayTabPage = new today_Fragment();
private Fragment createTabPage = new create_Fragment();
private Fragment myTabPage = new my_Fragment();
private FragmentManager fm;
// private LinearLayout mTodayTabPage;
// private LinearLayout mCreateTabPage;
// private LinearLayout mMyTabPage;
private LinearLayout mTodayTabButton;
private LinearLayout mCreateTabButton;
private LinearLayout mMyTabButton;
private ImageButton mTodayImageButton;
private ImageButton mCreateImageButton;
private ImageButton mMyImageButton;
private RecyclerView create_recycleview;
private List<String> list;
private Context context;
private create_Adapter create_Adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE); //没有标题栏
setContentView(R.layout.activity_main);
initView();
initFragment();
selectFragment(0);
initEvent();
context = this;
create_recycleview = findViewById(R.id.create_recycleview);
list = new ArrayList<String>();
for(int i=0;i<9;i++) {
list.add("这是第"+i+"个");
}
LinearLayoutManager manager = new LinearLayoutManager(context);
manager.setOrientation(LinearLayoutManager.VERTICAL);
create_Adapter = new create_Adapter(context,list);
create_recycleview.setAdapter(create_Adapter);
create_recycleview.setLayoutManager(manager);
}
private void initFragment() {
fm = getFragmentManager();
FragmentTransaction transaction = fm.beginTransaction();
transaction.add(R.id.id_content,todayTabPage);
transaction.add(R.id.id_content,createTabPage);
transaction.add(R.id.id_content,myTabPage);
transaction.commit();
}
private void initView() {
// mTodayTabPage = (LinearLayout) findViewById(R.id.id_tabPage_today);
// mCreateTabPage = (LinearLayout) findViewById(R.id.id_tabPage_create);
// mMyTabPage = (LinearLayout) findViewById(R.id.id_tabPage_create);
mTodayImageButton=(ImageButton) findViewById(R.id.id_imgButton_today);
mCreateImageButton=(ImageButton) findViewById(R.id.id_imgButton_create);
mMyImageButton=(ImageButton) findViewById(R.id.id_imgButton_my);
mTodayTabButton = (LinearLayout)findViewById(R.id.id_tabButton_today);
mCreateTabButton = (LinearLayout)findViewById(R.id.id_tabButton_create);
mMyTabButton = (LinearLayout)findViewById(R.id.id_tabButton_my);
}
private void hideFragment(FragmentTransaction transaction) {
transaction.hide(todayTabPage);
transaction.hide(createTabPage);
transaction.hide(myTabPage);
}
private void selectFragment(int i) {
FragmentTransaction transaction = fm.beginTransaction();
hideFragment(transaction);
switch (i) {
case 0:
transaction.show(todayTabPage);
mTodayImageButton.setImageResource(R.drawable.tab_sun);
break;
case 1:
transaction.show(createTabPage);
mCreateImageButton.setImageResource(R.drawable.tab_create);
break;
case 2:
transaction.show(myTabPage);
mMyImageButton.setImageResource(R.drawable.tab_my);
break;
default:
break;
}
transaction.commit();
}
@Override
public void onClick(View v) {
resetImg();
switch (v.getId()) {
case R.id.id_tabButton_today:
selectFragment(0);
break;
case R.id.id_tabButton_create:
selectFragment(1);
break;
case R.id.id_tabButton_my:
selectFragment(2);
break;
default:
break;
}
}
private void initEvent() {
mTodayTabButton.setOnClickListener(this);
mCreateTabButton.setOnClickListener(this);
mMyTabButton.setOnClickListener(this);
}
public void resetImg() {
mTodayImageButton.setImageResource(R.drawable.tab_sun2);
mCreateImageButton.setImageResource(R.drawable.tab_create2);
mMyImageButton.setImageResource(R.drawable.tab_my2);
}
}
//create_Adapter.java
package com.e.spirit;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.List;
public class create_Adapter extends RecyclerView.Adapter<create_Adapter.myviewholder> {
private List<String>list;
private Context context;
private View inflater;
public create_Adapter(Context context,List<String> list) {
this.context = context;
this.list = list;
}
@Override
public create_Adapter.myviewholder onCreateViewHolder( ViewGroup viewGroup, int i) {
inflater = LayoutInflater.from(context).inflate(R.layout.create_item,viewGroup,false);
myviewholder myviewholder = new myviewholder(inflater);
return myviewholder;
}
@Override
public void onBindViewHolder(create_Adapter.myviewholder myviewholder, int j) {
myviewholder.textView.setText(list.get(j));
}
@Override
public int getItemCount() {
return list.size();
}
class myviewholder extends RecyclerView.ViewHolder {
TextView textView;
public myviewholder(@NonNull View itemView) {
super(itemView);
textView = itemView.findViewById(R.id.create_textView);
}
}
}
三、那些没有技术含量的坑
Manifest merger failed : Attribute application@appComponentFactory value=(androidx.core.app.CoreComponentFactory) from [androidx.core:core:1.0.1] AndroidManifest.xml:22:18-86
is also present at [com.android.support:support-compat:28.0.0] AndroidManifest.xml:22:18-91 value=(android.support.v4.app.CoreComponentFactory).
Suggestion: add 'tools:replace="android:appComponentFactory"' to <application> element at AndroidManifest.xml:5:5-21:19 to override.
Android 和 AndroidX的兼容问题 :https://www.cnblogs.com/qiuniao/p/12191467.html