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:orientation="vertical" tools:context="com.example.mmaster.m_1_myapplication.Main2Activity"> <RelativeLayout android:layout_width="match_parent" android:layout_height="54dp" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="首页" android:textSize="30sp" /> RelativeLayout> <FrameLayout android:id="@+id/frament" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1"> FrameLayout> <RadioGroup android:id="@+id/redo" android:layout_width="match_parent" android:layout_height="54dp" android:layout_gravity="bottom" android:orientation="horizontal"> <RadioButton android:id="@+id/lie_biao" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:button="@null" android:gravity="center" android:text="列表" /> <RadioButton android:id="@+id/xiang_qing" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:button="@null" android:gravity="center" android:text="详情" /> RadioGroup> LinearLayout>
java代码:俩页面切换不用适配器嘀嘀嘀嘀嘀
package com.example.mmaster.m_1_myapplication; import android.os.Bundle; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentTransaction; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.FrameLayout; import android.widget.RadioButton; import android.widget.RadioGroup; import com.example.mmaster.m_1_myapplication.fragment.AFragment; import com.example.mmaster.m_1_myapplication.fragment.BFragment; public class Main2Activity extends AppCompatActivity implements View.OnClickListener { private FragmentManager supportFragmentManager; //执行者调全局 private FragmentTransaction fragmentTransaction; //调全局FrameLayout控件 public FrameLayout frament; private AFragment aFragment; private BFragment bFragment; private RadioButton lie_biao; private RadioButton xiang_qing; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2);//注意改变Main入口 initView(); initData(); } private void initData() { aFragment = new AFragment(); bFragment = new BFragment(); supportFragmentManager = getSupportFragmentManager(); fragmentTransaction = supportFragmentManager.beginTransaction(); fragmentTransaction.add(R.id.frament,aFragment); fragmentTransaction.commit(); } private void initView() { lie_biao = (RadioButton) findViewById(R.id.lie_biao); xiang_qing = (RadioButton) findViewById(R.id.xiang_qing); lie_biao.setOnClickListener(this); xiang_qing.setOnClickListener(this); frament = (FrameLayout) findViewById(R.id.frament); frament.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.lie_biao: //管理者 执行者 //替换 //提交 getSupportFragmentManager().beginTransaction().replace(R.id.frament,aFragment).commit(); break; case R.id.xiang_qing: //管理者 执行者 //替换 //提交 getSupportFragmentManager().beginTransaction().replace(R.id.frament,bFragment).commit(); break; } } }
AFregment java代码:实体Bean联系到数据库
package com.example.mmaster.m_1_myapplication.fragment; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentTransaction; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.FrameLayout; import com.example.mmaster.m_1_myapplication.App; import com.example.mmaster.m_1_myapplication.R; import com.example.mmaster.m_1_myapplication.StudentBean; import com.example.mmaster.m_1_myapplication.adapter.MyAdapter; import com.example.mmaster.m_1_myapplication.bean.Bean; import com.google.gson.Gson; import java.io.IOException; import java.util.ArrayList; import java.util.List; import app.jiyun.com.testsqlitegd.greendao.StudentBeanDao; import okhttp3.Call; import okhttp3.Callback; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.Response; /** * A simple {@link Fragment} subclass. */ public class AFragment extends Fragment { private String url = "http://192.168.43.191:8080/data.json"; private RecyclerView recy; private Liststudent; private Handler handler = new Handler() { private MyAdapter myAdapter; @Override public void handleMessage(Message msg) { super.handleMessage(msg); String json = (String) msg.obj; Bean bean = new Gson().fromJson(json, Bean.class); student = bean.getStudents().getStudent(); LinearLayoutManager manager = new LinearLayoutManager(getContext()); recy.setLayoutManager(manager); myAdapter = new MyAdapter(student, getContext()); recy.setAdapter(myAdapter); myAdapter.setOnCliK(new MyAdapter.OnClik() { @Override public void OnCliklistener(int position) { String image = student.get(position).getImg(); String content = student.get(position).getContent(); String name = student.get(position).getName(); StudentBean studentBean = new StudentBean(null, name, content, image); dao.insert(studentBean); //加入到数据库 } }); } }; private StudentBeanDao dao; public AFragment() { // Required empty public constructor } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View inflate = inflater.inflate(R.layout.fragment_a, container, false); //数据库 dao = App.getMyApp().getDaoSession().getStudentBeanDao(); initView(inflate); initData(); return inflate; } private void initData() { OkHttpClient build = new OkHttpClient.Builder().build(); Request request = new Request.Builder().url(url).build(); build.newCall(request).enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { } @Override public void onResponse(Call call, Response response) throws IOException { String string = response.body().string(); handler.obtainMessage(1, string).sendToTarget(); } }); } private void initView(View inflate) { recy = (RecyclerView) inflate.findViewById(R.id.recy); } }
BFragment java代码:
package com.example.mmaster.m_1_myapplication.fragment; import android.content.Intent; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import com.example.mmaster.m_1_myapplication.App; import com.example.mmaster.m_1_myapplication.MainXiang_Activity; import com.example.mmaster.m_1_myapplication.R; import com.example.mmaster.m_1_myapplication.StudentBean; import com.example.mmaster.m_1_myapplication.adapter.MyAdapter; import com.squareup.picasso.Picasso; import java.util.List; import app.jiyun.com.testsqlitegd.greendao.StudentBeanDao; /** * A simple {@link Fragment} subclass. */ public class BFragment extends Fragment { public static String string1; public static String string; public static String string2; private RecyclerView recy; private StudentBeanDao dao1; private MyAdapter myAdapter; private ListstudentBeans; public BFragment() { // Required empty public constructor } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment View inflate = inflater.inflate(R.layout.fragment_b, container, false); initView(inflate); dao1 = App.getMyApp().getDaoSession().getStudentBeanDao(); studentBeans = dao1.loadAll(); myAdapter = new MyAdapter(studentBeans, getContext()); recy.setAdapter(myAdapter); initData(); return inflate; } private void initData() { myAdapter.setOnCliK(new MyAdapter.OnClik() { @Override public void OnCliklistener(int position) { string = studentBeans.get(position).getContent().toString(); string1 = studentBeans.get(position).getName().toString(); string2 = studentBeans.get(position).getImg().toString(); Intent intent = new Intent(getContext(), MainXiang_Activity.class); startActivity(intent); } }); } private void initView(View inflate) { recy = (RecyclerView) inflate.findViewById(R.id.recy); recy.setLayoutManager(new LinearLayoutManager(getContext())); } }
详情页面 java代码:
package com.example.mmaster.m_1_myapplication; import android.content.Intent; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.widget.ImageView; import android.widget.TextView; import com.example.mmaster.m_1_myapplication.fragment.BFragment; import com.squareup.picasso.Picasso; public class MainXiang_Activity extends AppCompatActivity { private TextView content; private TextView name; private ImageView img; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main_xiang_); initView(); Intent intent = getIntent();//用不到不用写
content.setText(BFragment.string); name.setText(BFragment.string1); Picasso.with(this).load(BFragment.string2).into(img);} private void initView() { content = (TextView) findViewById(R.id. content); name = (TextView) findViewById(R.id. name); img = (ImageView) findViewById(R.id. img); }}