• 网络请求展示数据


    public interface GankApi {
        @GET("api/data/Android/{size}/{page}")
        Call<GankData<GankFeed>> list(@Path("size") int size,@Path("page") int page);
    

      

    public class Myadapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
        private Context context;
        private List<GankFeed> list;
        private View view;
    
    
        public Myadapter(Context context, List<GankFeed> list) {
            this.context = context;
            this.list = list;
        }
    
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            if (viewType == 0){
                view = LayoutInflater.from(context).inflate(R.layout.item_first, parent, false);
                myOne myOne = new myOne(view);
                return myOne;
            }else{
                view = LayoutInflater.from(context).inflate(R.layout.item_second, parent, false);
                myTwo myTwo = new myTwo(view);
                return myTwo;
            }
        }
    
        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
                if(holder instanceof myOne){
                   myOne holder1 = (myOne) holder;
                    GankFeed feed = list.get(position);
                    holder1.tv.setText(feed.getType());
                    holder1.tv2.setText(feed.getPublishedAt());
                }else if(holder instanceof myTwo){
                    myTwo two = (myTwo) holder;
                    GankFeed gankFeed = list.get(position);
                    two.tv.setText(gankFeed.getType());
                    two.tv2.setText(gankFeed.getPublishedAt());
                    ImageLoader.getInstance().displayImage(gankFeed.getImages().toString(),two.imageView);
                }
        }
    
        @Override
        public int getItemViewType(int position) {
            GankFeed gankFeed = list.get(position);
            List<String> images = gankFeed.getImages();
            if(images == null){
                return 0;
            }else {
                return 1;
            }
    
        }
    
    
        @Override
        public int getItemCount() {
            return list.size();
        }
        class myOne extends RecyclerView.ViewHolder{
        @BindView(R.id.tv1)
             TextView tv;
        @BindView(R.id.tv2)
             TextView tv2;
    
            public myOne(View itemView) {
                super(itemView);
                ButterKnife.bind(this,itemView);
            }
        }
        class myTwo extends RecyclerView.ViewHolder{
            @BindView(R.id.second1)
            TextView tv;
            @BindView(R.id.second2)
             TextView tv2;
            @BindView(R.id.iv)
            ImageView imageView;
    
            public myTwo(View itemView) {
                super(itemView);
                ButterKnife.bind(this,itemView);
    
            }
        }
    }
    

      

    public class BaseClass extends AppCompatActivity {
    
    
        private Unbinder bind;
    
        @Override
        public void setContentView(View view) {
            super.setContentView(view);
            bind = ButterKnife.bind(this);
        }
    
        @Override
        protected void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
        }
    
        @Override
        protected void onDestroy() {
            super.onDestroy();
            if (bind!= null){
                bind.unbind();
            }
    
        }
    }
    

      

    public class FragmentOne extends Fragment{
    
        private RecyclerView recyclerView;
    
        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.fragment_one, container, false);
    
            RecyclerView.LayoutManager manager = new LinearLayoutManager(getContext());
            recyclerView = view.findViewById(R.id.rrv);
            //布局加载
            recyclerView.setLayoutManager(manager);
            Retrofit retrofit = new Retrofit.Builder()
                    .baseUrl("http://gank.io/")
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
            final GankApi gankApi = retrofit.create(GankApi.class);
            Call<GankData<GankFeed>> list = gankApi.list(10, 1);
            DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(getContext(),"notes-db");
            Database db = helper.getWritableDb();
            final DaoSession daoSession = new DaoMaster(db).newSession();
            list.enqueue(new Callback<GankData<GankFeed>>() {
                @Override
                public void onResponse(Call<GankData<GankFeed>> call, Response<GankData<GankFeed>> response) {
                    GankFeedDao gankFeedDao = daoSession.getGankFeedDao();
                    for (GankFeed feed:response.body().results) {
                        gankFeedDao.insert(feed);
                    }
                    List<GankFeed> results = response.body().results;
                    Myadapter adapter = new Myadapter(getContext(),results);
                    recyclerView.setAdapter(adapter);
                }
    
                @Override
                public void onFailure(Call<GankData<GankFeed>> call, Throwable t) {
    
                }
            });
            return view;
        }
    }
    

      

    public class ImageTypeConverter implements PropertyConverter<List<String>, String> {
        @Override
        public List<String> convertToEntityProperty(String databaseValue) {
            ArrayList<String> list = new ArrayList<>();
            list.add(databaseValue);
            return list;
        }
    
        @Override
        public String convertToDatabaseValue(List<String> entityProperty) {
            return (entityProperty == null && !entityProperty.isEmpty()) ? "" : entityProperty.get(0);
        }
    }
    

      

    public class MainActivity extends BaseClass {
    
    
        @BindView(R.id.frg)
        FrameLayout mFrg;
        @BindView(R.id.bt1)
        RadioButton mBt1;
        @BindView(R.id.bt2)
        RadioButton mBt2;
        @BindView(R.id.bt3)
        RadioButton mBt3;
        @BindView(R.id.bt4)
        RadioButton mBt4;
        @BindView(R.id.bt5)
        RadioButton mBt5;
        @BindView(R.id.rg)
        RadioGroup mRg;
        private List<Fragment> fragmentList;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            ButterKnife.bind(this);
            //实例化
            fragmentList = new ArrayList<>();
            //添加fragment
            addFragment();
            //设置默认
            mRg.check(R.id.bt1);
            switchFragmnet(0);
    
        }
    
        @OnClick({R.id.bt1, R.id.bt2, R.id.bt3, R.id.bt4, R.id.bt5, R.id.rg, R.id.frg})
        public void onClick(View v) {
            switch (v.getId()) {
                default:
                    break;
                case R.id.bt1:
                    switchFragmnet(0);
                    break;
                case R.id.bt2:
                    switchFragmnet(1);
                    break;
                case R.id.bt3:
                    switchFragmnet(2);
                    break;
                case R.id.bt4:
                    switchFragmnet(3);
                    break;
                case R.id.bt5:
                    switchFragmnet(4);
                    break;
                case R.id.rg:
                    break;
                case R.id.frg:
                    break;
            }
        }
    
        private void addFragment() {
            fragmentList.add(new FragmentOne());
            fragmentList.add(new FragmentTwo());
            fragmentList.add(new FragmentThree());
            fragmentList.add(new FragmentFour());
            fragmentList.add(new FragmentFive());
    
        }
    
        private void switchFragmnet(int position) {
            //快开启一个事务
            FragmentTransaction fragmentManager = getSupportFragmentManager().beginTransaction();
            for (int i = 0; i < fragmentList.size(); i++) {
                Fragment fragment = fragmentList.get(i);
                if (i == position) {
                    //显示fragment
                    if (fragment.isAdded()) {
                        //如果被事务添加,显示
                        fragmentManager.show(fragment);
                    } else {
                        //如果这个fragment没有被事务添加过,添加
                        fragmentManager.add(R.id.frg, fragment);
                    }
    
                }else{
                    //隐藏fragment
                    if (fragment.isAdded()) {
                        //如果这个fragment已经被事务添加,隐藏
                        fragmentManager.hide(fragment);
                    }
                }
    
            }
            fragmentManager.commit();
    
        }
    }
    

      item_second

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    
        <LinearLayout
            android:layout_margin="10dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
            <TextView
                android:id="@+id/second1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="1"/>
            <TextView
                android:layout_marginTop="10dp"
                android:id="@+id/second2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="2"/>
        </LinearLayout>
    <ImageView
        android:id="@+id/iv"
        android:layout_marginRight="10dp"
        android:layout_alignParentRight="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@mipmap/ic_launcher"/>
    
    </RelativeLayout>
    

      item_first

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <TextView
            android:layout_margin="10dp"
            android:id="@+id/tv1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="3"/>
        <TextView
            android:layout_margin="10dp"
            android:id="@+id/tv2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="4"/>
    
    </LinearLayout>
    

      one

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    <android.support.v7.widget.RecyclerView
        android:id="@+id/rrv"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </android.support.v7.widget.RecyclerView>
    
    </LinearLayout>
    

      

  • 相关阅读:
    如何使用Java、Servlet创建二维码
    Java线程池主线程等待子线程执行完成
    Java多线程--让主线程等待所有子线程执行完毕
    查询及删除重复记录的方法
    聚集索引和非聚集索引
    数据库索引类型及实现方式
    各种排序算法的分析及java实现
    两个变量交换值的方法
    Java性能优化技巧
    JVM调优总结(九)-新一代的垃圾回收算法
  • 原文地址:https://www.cnblogs.com/wsq110/p/7975425.html
Copyright © 2020-2023  润新知