RecyclerView 結合cardview和materia degisn通過retrofit的一個專案,山寨it之家

木土其發表於2019-03-11

看到大家都習慣寫部落格,我也來寫自己的第一個部落格吧,也算是對專案的備份。

首先ui部分

 

佈局如圖所示

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
    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:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <include
        layout="@layout/tablayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="250dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_main"
        app:menu="@menu/activity_main_drawer" />

</android.support.v4.widget.DrawerLayout>

 

itms.xml:

 

<?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="@dimen/cardview_layout_hight"


    xmlns:app="http://schemas.android.com/apk/res-auto"

  >

    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentStart="true"
        android:id="@+id/cardView_news"
        android:background="#dddddd"
        android:layout_margin="7dp"

        android:paddingTop="1dp"
        android:layout_alignParentTop="true">


        <com.dqj.fakeithomes.siv.SmartImageView
            android:id="@+id/news_img"
            android:layout_width="@dimen/cardview_img_withhig"

            android:layout_height="@dimen/cardview_img_withhig"
        android:layout_marginLeft="20dp"
            android:layout_marginTop="20dp"
            android:elevation="50dp"
            android:src="@drawable/nv_bg"
            android:transitionName="img" />

        <TextView
            android:id="@+id/news_title"
            android:layout_width="200dp"
            android:layout_height="102dp"
            android:layout_marginLeft="160dp"
            android:layout_marginTop="10dp"
            android:background="#1a000000"
            android:fontFamily="sans-serif"
            android:maxLength="34"
            android:text="小米今日釋出小米mix3:高通845 屏佔比99% 售價1999起"
            android:textColor="#464445"

            android:textSize="@dimen/news_title_size"
            android:transitionName="title" />

        <TextView
            android:id="@+id/news_from"
            android:layout_width="@dimen/news_from_width"
            android:layout_height="@dimen/news_from_heigh"
            android:layout_marginLeft="160dp"
            android:layout_marginTop="80dp"
            android:gravity="center"
            android:maxLength="7"
            android:text="it之家"
            android:textColor="#d61200"
            android:textSize="@dimen/news_from_size"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/news_time"
            android:layout_width="@dimen/news_time_width"
            android:layout_height="@dimen/news_time_heigh"
          android:layout_marginLeft="200dp"
            android:layout_marginTop="110dp"
            android:fontFamily="sans-serif"
            android:gravity="center"
            android:text="12:24 12-30 2019"
            android:textColor="#838383"
            android:textSize="@dimen/news_time_size" />


    </android.support.v7.widget.CardView>

</RelativeLayout>
nav_header_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="@dimen/nav_header_height"
    android:background="@drawable/nv_bg"
    android:orientation="vertical"
    android:theme="@style/ThemeOverlay.AppCompat.Dark">






    <ImageView
        android:backgroundTint="#177c6f"
        android:id="@+id/Login_ico"
        android:textSize="30dp"
        app:elevation="20dp"
        android:layout_width="130dp"
        android:layout_height="130dp"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        tools:ignore="MissingPrefix" />


    <Button

        android:id="@+id/btn_login"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:backgroundTint="#3b8e8d"

        android:text="@string/onclick_to_login"
        android:layout_alignParentBottom="true"
        android:layout_alignParentStart="true" />




</RelativeLayout>

 

news_content_fragment.xml:



<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="@dimen/nav_header_height"
    android:background="@drawable/nv_bg"
    android:orientation="vertical"
    android:theme="@style/ThemeOverlay.AppCompat.Dark">






    <ImageView
        android:backgroundTint="#177c6f"
        android:id="@+id/Login_ico"
        android:textSize="30dp"
        app:elevation="20dp"
        android:layout_width="130dp"
        android:layout_height="130dp"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        tools:ignore="MissingPrefix" />


    <Button

        android:id="@+id/btn_login"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:backgroundTint="#3b8e8d"

        android:text="@string/onclick_to_login"
        android:layout_alignParentBottom="true"
        android:layout_alignParentStart="true" />




</RelativeLayout>

 


r

ecycler_view_fragment.xml:



<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="@dimen/nav_header_height"
    android:background="@drawable/nv_bg"
    android:orientation="vertical"
    android:theme="@style/ThemeOverlay.AppCompat.Dark">






    <ImageView
        android:backgroundTint="#177c6f"
        android:id="@+id/Login_ico"
        android:textSize="30dp"
        app:elevation="20dp"
        android:layout_width="130dp"
        android:layout_height="130dp"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        tools:ignore="MissingPrefix" />


    <Button

        android:id="@+id/btn_login"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:backgroundTint="#3b8e8d"

        android:text="@string/onclick_to_login"
        android:layout_alignParentBottom="true"
        android:layout_alignParentStart="true" />




</RelativeLayout>

 

tablayout.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="@dimen/nav_header_height"
    android:background="@drawable/nv_bg"
    android:orientation="vertical"
    android:theme="@style/ThemeOverlay.AppCompat.Dark">






    <ImageView
        android:backgroundTint="#177c6f"
        android:id="@+id/Login_ico"
        android:textSize="30dp"
        app:elevation="20dp"
        android:layout_width="130dp"
        android:layout_height="130dp"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        tools:ignore="MissingPrefix" />


    <Button

        android:id="@+id/btn_login"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:backgroundTint="#3b8e8d"

        android:text="@string/onclick_to_login"
        android:layout_alignParentBottom="true"
        android:layout_alignParentStart="true" />




</RelativeLayout>

 

在網路方面,用的是it之家的api介面,不是公開的,是我抓包抓的哈哈,侵權刪!

 

 

json協議如圖

 

新聞列表 https://api.ithome.com/json/newslist/news?r=0
文章詳情 https://api.ithome.com/xml/newscontent/350/412.xml
相關文章 https://api.ithome.com/json/tags/0350/350362.json
最熱評論 https://dyn.ithome.com/json/hotcommentlist/350/87a8e5b144d81938.json
評論列表 https://dyn.ithome.com/json/commentlist/350/87a8e5b144d81938.json
評論詳情 https://dyn.ithome.com/json/commentcontent/d739ee8f2ceb0a27.json
輪播新聞 https://api.ithome.com/xml/slide/slide.xml
圈子列表 https://apiquan.ithome.com/api/post?categoryid=0&type=0&orderTime=&visistCount&pageLength
圈子詳情 https://apiquan.ithome.com/api/post/236076
圈子評論 https://apiquan.ithome.com/api/reply?postid=236076&replyidlessthan=3241294

 

/*
 * newsid : 413374
 * title : 小米王騰:無線充電滑鼠墊已在路上,還能更酷
 * postdate : 2019-03-09T23:54:50.357
 * orderdate : 2019-03-09T23:54:50.357
 * description : 小米9手機搭載了全球首款20W無線閃充,堪比有線快充。現在小米產品總監王騰微博表示,支援無線充電的大滑鼠墊已經在路上了,相比網友的一些想法,可能更酷一些。
 * image : http://img.ithome.com/newsuploadfiles/thumbnail/2019/3/413374_240.jpg?r=1552146890357
 * hitcount : 9849
 * commentcount : 101
 * cid : 71
 * sid : 0
 * url : /0/413/374.htm
 * v : 001
 * lapinid : 1829277
 * imagelist : ["http://img.ithome.com/newsuploadfiles/2019/3/20190309_210543_50.jpg@s_2,w_240,h_180","http://img.ithome.com/newsuploadfiles/2019/3/20190306_004559_435.png@s_2,w_240,h_180","http://img.ithome.com/newsuploadfiles/2019/3/20190306_004634_944.png@s_2,w_240,h_180"]
 */

json解析用的是fastjson這個外掛

bean.class如下

package com.dqj.fakeithomes;

import java.util.List;

public class bean {




    private boolean lapin;
    private List<?> toplist;
    private List<NewslistBean> newslist;

    public boolean isLapin() {
        return lapin;
    }

    public void setLapin(boolean lapin) {
        this.lapin = lapin;
    }

    public List<?> getToplist() {
        return toplist;
    }

    public void setToplist(List<?> toplist) {
        this.toplist = toplist;
    }

    public List<NewslistBean> getNewslist() {
        return newslist;
    }

    public void setNewslist(List<NewslistBean> newslist) {
        this.newslist = newslist;
    }

    public static class NewslistBean {
      public   NewslistBean(String title,int sid,String postdate,String image){
          this.title=title;
          this.postdate=postdate;
          this.image=image;
          this.sid=sid;



      }
        /**
         * newsid : 413374
         * title : 小米王騰:無線充電滑鼠墊已在路上,還能更酷
         * postdate : 2019-03-09T23:54:50.357
         * orderdate : 2019-03-09T23:54:50.357
         * description : 小米9手機搭載了全球首款20W無線閃充,堪比有線快充。現在小米產品總監王騰微博表示,支援無線充電的大滑鼠墊已經在路上了,相比網友的一些想法,可能更酷一些。
         * image : http://img.ithome.com/newsuploadfiles/thumbnail/2019/3/413374_240.jpg?r=1552146890357
         * hitcount : 9849
         * commentcount : 101
         * cid : 71
         * sid : 0
         * url : /0/413/374.htm
         * v : 001
         * lapinid : 1829277
         * imagelist : ["http://img.ithome.com/newsuploadfiles/2019/3/20190309_210543_50.jpg@s_2,w_240,h_180","http://img.ithome.com/newsuploadfiles/2019/3/20190306_004559_435.png@s_2,w_240,h_180","http://img.ithome.com/newsuploadfiles/2019/3/20190306_004634_944.png@s_2,w_240,h_180"]
         */

        private int newsid;
        private String title;
        private String postdate;
        private String orderdate;
        private String description;
        private String image;
        private int hitcount;
        private int commentcount;
        private int cid;
        private int sid;
        private String url;
        private String v;
        private int lapinid;
        private List<String> imagelist;

        public int getNewsid() {
            return newsid;
        }

        public void setNewsid(int newsid) {
            this.newsid = newsid;
        }

        public String getTitle() {
            return title;
        }

        public void setTitle(String title) {
            this.title = title;
        }

        public String getPostdate() {
            return postdate;
        }

        public void setPostdate(String postdate) {
            this.postdate = postdate;
        }

        public String getOrderdate() {
            return orderdate;
        }

        public void setOrderdate(String orderdate) {
            this.orderdate = orderdate;
        }

        public String getDescription() {
            return description;
        }

        public void setDescription(String description) {
            this.description = description;
        }

        public String getImage() {
            return image;
        }

        public void setImage(String image) {
            this.image = image;
        }

        public int getHitcount() {
            return hitcount;
        }

        public void setHitcount(int hitcount) {
            this.hitcount = hitcount;
        }

        public int getCommentcount() {
            return commentcount;
        }

        public void setCommentcount(int commentcount) {
            this.commentcount = commentcount;
        }

        public int getCid() {
            return cid;
        }

        public void setCid(int cid) {
            this.cid = cid;
        }

        public int getSid() {
            return sid;
        }

        public void setSid(int sid) {
            this.sid = sid;
        }

        public String getUrl() {
            return url;
        }

        public void setUrl(String url) {
            this.url = url;
        }

        public String getV() {
            return v;
        }

        public void setV(String v) {
            this.v = v;
        }

        public int getLapinid() {
            return lapinid;
        }

        public void setLapinid(int lapinid) {
            this.lapinid = lapinid;
        }

        public List<String> getImagelist() {
            return imagelist;
        }

        public void setImagelist(List<String> imagelist) {
            this.imagelist = imagelist;
        }
    }
}


然後是retrofit2的介面

public interface DoRequset {
@GET("news")
    Call<bean> getCall();

}


MainActivity如下:
public class MainActivity extends AppCompatActivity  implements NavigationView.OnNavigationItemSelectedListener {
    public Handler Mhandler = new Handler() {
        public void handleMessage(Message message) {
            String a = (String) message.obj;
            switch (message.what) {
                case 1:
                    drawer.openDrawer(Gravity.START);
                    break;
                case 2:
                    Toast.makeText(MainActivity.this, a + "00", Toast.LENGTH_LONG).show();

            }
        }
    };
    DrawerLayout drawer;
    List<bean.NewslistBean> arrayList=new ArrayList<>();

    recFragmnet mrecFragmnet=null;
    webViewFragment mwebViewFragment=null;
    private fragmentAdapter mSectionsPagerAdapter;

    /**
     * The {@link ViewPager} that will host the section contents.
     */
    private ViewPager mViewPager;
public void requset(){
    Retrofit retrofit=new Retrofit.Builder()
            .baseUrl("https://api.ithome.com/json/newslist/")
            .addConverterFactory(GsonConverterFactory.create())
            .build();
    DoRequset doRequset=retrofit.create(DoRequset.class);
    Call<bean> call=doRequset.getCall();
    call.enqueue(new Callback<bean>() {
        @Override
        public void onResponse(Call<bean> call, Response<bean> response) {
             arrayList = response.body().getNewslist();
            mrecFragmnet.recyclerView.setLayoutManager(new LinearLayoutManager(mrecFragmnet.recyclerView.getContext()));
            homeadpter holder=new homeadpter(mrecFragmnet.recyclerView.getContext(),arrayList);
            mrecFragmnet.recyclerView.setAdapter(holder);
            //Toast.makeText(MainActivity.this,  response.body().getNewslist().get(1).getDescription(),Toast.LENGTH_LONG).show();


        }

        @Override
        public void onFailure(Call<bean> call, Throwable t) {
            Toast.makeText(MainActivity.this, "error!!!2",Toast.LENGTH_LONG).show();

        }
    });


}

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        requset();
        List<Fragment> list=new ArrayList<>();

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        mViewPager = (ViewPager) findViewById(R.id.viewpager);
        mrecFragmnet=new recFragmnet();
        mrecFragmnet.dod(null);
        mwebViewFragment=new webViewFragment();
        list.add(mrecFragmnet);
        list.add(mwebViewFragment);
        mSectionsPagerAdapter = new fragmentAdapter(getSupportFragmentManager(),list);

        // Set up the ViewPager with the sections adapter.
        mViewPager.setAdapter(mSectionsPagerAdapter);

        TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);

        mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
        tabLayout.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(mViewPager));

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });

         drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.addDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);
        Mhandler.sendEmptyMessageDelayed(1, 700);

    }
public void showDW(){


}

    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();



        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }



    public class fragmentAdapter extends FragmentPagerAdapter {
        List<Fragment> list;

        public fragmentAdapter(FragmentManager fm, List list) {
            super(fm);
            this.list=list;
        }

        @Override
        public Fragment getItem(int position) {
            // getItem is called to instantiate the fragment for the given page.
            // Return a PlaceholderFragment (defined as a static inner class below).
            return list.get(position);
        }

        @Override
        public int getCount() {
            // Show 3 total pages.
            return list.size();
        }
    }


}

有些程式碼是模板自動生成的
recylerview的介面卡如下

 class homeadpter extends RecyclerView.Adapter<homeadpter.MyViewHolder> {
    Context context;
     List<bean.NewslistBean> list=new ArrayList<>();
    public homeadpter(Context context,List list)
    {
        this.context=context;
        this.list=list;
    }
     class MyViewHolder extends RecyclerView.ViewHolder{
         SmartImageView imageView;
         TextView title;
         TextView time;
         TextView from;
         int id;
         public MyViewHolder(@NonNull View itemView) {
             super(itemView);
             imageView=(SmartImageView) itemView.findViewById(R.id.news_img);
             title=(TextView) itemView.findViewById(R.id.news_title);
             time=(TextView)itemView.findViewById(R.id.news_time);
             from=(TextView)itemView.findViewById(R.id.news_from);

         }
     }

     @NonNull
     @Override
     public MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view=LayoutInflater.from(context).inflate(R.layout.item,viewGroup,false);
MyViewHolder myViewHolder=new MyViewHolder(view);

         return myViewHolder;
     }

     @NonNull


     public void onBindViewHolder(@NonNull MyViewHolder myViewHolder, int i) {
String[] strings=list.get(i).getPostdate().split("T");
String time=strings[1].substring(0,5)+" "+strings[0];
myViewHolder.from.setText(list.get(i).getSid()==0?"it之家":"位置");
myViewHolder.title.setText(list.get(i).getTitle());
myViewHolder.time.setText(time);
myViewHolder.imageView.setImageUrl(list.get(i).getImage());
     }




    @Override
    public int getItemCount() {
        return list.size();
    }
 }




webViewFragment:(這個還沒有完成,等待後續部落格更新)
package com.dqj.fakeithomes;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.dqj.fakeithomes.MainActivity;
import com.dqj.fakeithomes.R;

public class webViewFragment extends Fragment {
    /**
     * The fragment argument representing the section number for this
     * fragment.
     */
    private static final String ARG_SECTION_NUMBER = "section_number";

    public webViewFragment() {
    }

    /**
     * Returns a new instance of this fragment for the given section
     * number.
     */


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.news_content_fragment, container, false);
        return rootView;
    }
}

 

最後要忘記清單的許可權

說一下這個裡面的坑,

因為首先onCreate裡面會zhix執行網路操作,所有改變recylerview的操作也可以retorfit的

onResponse裡面進行
 arrayList = response.body().getNewslist();
mrecFragmnet.recyclerView.setLayoutManager(new LinearLayoutManager(mrecFragmnet.recyclerView.getContext()
/**
一定不要用getApllicationContext(),要用recyclerView.getContext()。這個一個找了很久的bug
**/));
homeadpter holder=new homeadpter(mrecFragmnet.recyclerView.getContext(),arrayList);
mrecFragmnet.recyclerView.setAdapter(holder);

第二就是bean類的gouz構造方法為靜態

public static class NewslistBean {
  public   NewslistBean(String title,int sid,String postdate,String image){
      this.title=title;
      this.postdate=postdate;
      this.image=image;
      this.sid=sid;



  }

最後 版權所有,api侵權請聯絡我,不支援轉發

相關文章