package com.cardvalue.sys.fragment;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
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.bartoszlipinski.recyclerviewheader.RecyclerViewHeader;
import com.cardvalue.sys.activity.LoadWebPage;
import com.cardvalue.sys.R;
import com.cardvalue.sys.adapter.MoreRecylerAdapter;
import com.cardvalue.sys.base.BaseFragment;
import com.cardvalue.sys.entity.MoreListDto;
import com.cardvalue.sys.newnetwork.Config;
import java.util.ArrayList;
import java.util.List;
import butterknife.Bind;
import butterknife.ButterKnife;
/**
* Recyler来取代listview 很多其它页面
* Created by cardvalue on 2016/4/5.
*/
public class MoreRecylerFragment extends BaseFragment {
String server = Config.getWeixinIp()+"";
private View view;
@Bind(R.id.recyclerView) RecyclerView recyclerView;
<span style="color:#cc0000;">@Bind(R.id.header) RecyclerViewHeader header;</span>
private List<MoreListDto> mMoreListDto= new ArrayList<>();
private MoreRecylerAdapter mMoreAdapter;
private int [] icons={R.mipmap.share,R.mipmap.pruoduce,R.mipmap.honor,
R.mipmap.contact,R.mipmap.about,R.mipmap.feedback,R.mipmap.service,R.mipmap.service};
private String[] title={"融资攻略","公司简单介绍","资质荣誉","联系方式","关于小企额","意见反馈","在线客服"};
private String[] activity={"","","","","com.cardvalue.sys.activity.AboutActivity",
"com.cardvalue.sys.activity.FeedBackActivity","com.cardvalue.sys.activity.CustomerServiceActivity"};
String url1=server+"new/m/more/question";
String url2=server+"/new/m/more/aboutUs?
isApp=1"; String url3=server+"/new/m/more/honor?isApp=1"; String url4=server+"/new/m/more/contactUs?isApp=1"; private String[] url={url1,url2,url3,url4,"","",""}; @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { view=inflater.inflate(R.layout.fragment_more_recyler,container,false); ButterKnife.bind(this,view); // 创建一个线性布局管理器 LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity()); layoutManager.setOrientation(<span style="color:#cc0000;">LinearLayoutManager.VERTICAL</span>);<span style="color:#ff0000;">//设置垂直 还是水平</span> // 设置布局管理器 recyclerView.setLayoutManager(layoutManager); <span style="color:#990000;"> </span><span style="color:#cc0000;"> header.attachTo(recyclerView, true);//加入header</span> view.findViewById(R.id.as).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(); intent.setAction("android.intent.action.CALL"); intent.addCategory("android.intent.category.DEFAULT"); //指定要拨打的电话号码 intent.setData(Uri.parse("tel:" + "4008-803-803")); startActivity(intent); } }); for(int i=0;i<title.length;i++){ MoreListDto item=new MoreListDto(); item.setTitle(title[i]); item.setIconIndex(icons[i]); item.setActivity(activity[i]); item.setUrl(url[i]); if (i == 0 || i == 1) { item.setShow(true); } mMoreListDto.add(item); } <span style="color:#cc0000;"> mMoreAdapter = new MoreRecylerAdapter(getActivity(),mMoreListDto); recyclerView.setAdapter(mMoreAdapter);</span> mMoreAdapter.setItemClickListener(new MoreRecylerAdapter.OnItemClickListener() { @Override public void onItemClick(View v, int position) { // MoreListDto item=(MoreListDto)mListView.getItemAtPosition(position); MoreListDto item =mMoreListDto.get(position); if(!item.getActivity().equals("")){ try { Intent intent = new Intent(getActivity(),Class.forName(item.getActivity().toString())); startActivity(intent); } catch (ClassNotFoundException e) { e.printStackTrace(); } }else if(!item.getUrl().equals("")){ Intent intent = new Intent(getActivity(),LoadWebPage.class); intent.putExtra("title", item.getTitle().toString()); intent.putExtra("url", item.getUrl().toString()); startActivity(intent); } } }); return view; } @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { setHeaderFields(0, R.string.more,0,0, 0,0); super.onActivityCreated(savedInstanceState); } }
<pre name="code" class="java">package com.cardvalue.sys.adapter;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.cardvalue.sys.R;
import com.cardvalue.sys.entity.MoreListDto;
import java.util.ArrayList;
import java.util.List;
/**
* 很多其它页面的adapter
* Created by cardvalue on 2016/4/1.
*/
public class MoreRecylerAdapter extends RecyclerView.Adapter{
private List<MoreListDto> mMoreListDto= new ArrayList<>();
private Context context;
private LayoutInflater inflater;
public MoreRecylerAdapter(Context context, List<MoreListDto> mMoreListDto){//Context context,
this.context=context;
this.mMoreListDto=mMoreListDto;
this.inflater=LayoutInflater.from(context);
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = View.inflate(context,R.layout.list_more,null);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
if(holder instanceof ViewHolder){
ViewHolder viewHolder=(ViewHolder)holder;
MoreListDto moreListDto =mMoreListDto.get(position) ;
if(moreListDto!=null){
if(moreListDto.isShow()){
viewHolder.asd.setVisibility(View.VISIBLE);
}else{
viewHolder.asd.setVisibility(View.GONE);
}
if(moreListDto.getTitle().equals("融资攻略")||moreListDto.getTitle().equals("在线客服")){
viewHolder.bottom.setVisibility(View.VISIBLE);
}
viewHolder.image1.setImageResource(moreListDto.getIconIndex());
viewHolder.text1.setText(moreListDto.getTitle());
}
}
}
@Override
public int getItemCount() {
return mMoreListDto!=null?mMoreListDto.size():0;
}
class ViewHolder extends RecyclerView.ViewHolder{
ImageView image1;
TextView text1;
RelativeLayout root;
View asd;
View bottom;
public ViewHolder(View itemView) {
super(itemView);
init(itemView);
//给列表item点击点击事件
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(itemClickListener!=null){
itemClickListener.onItemClick(v,getLayoutPosition());
}
}
});
}
private void init(View view){
image1 = (ImageView) view.findViewById(R.id.image1);
text1 = (TextView) view.findViewById(R.id.text1);
root = (RelativeLayout) view.findViewById(R.id.root);
asd= view.findViewById(R.id.and);
bottom= view.findViewById(R.id.bottom);
}
}
//对外部暴漏点击事件接口
public interface OnItemClickListener{
void onItemClick(View v,int position);
}
public OnItemClickListener itemClickListener;
public void setItemClickListener(OnItemClickListener itemClickListener) {
this.itemClickListener = itemClickListener;
}
}
<?xml version="1.0" encoding="utf-8"?
> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:background="@color/background_color" tools:context="com.cardvalue.sys.fragment.BasicFragment"> <include android:id="@+id/include_title" layout="@layout/include_title"/> <FrameLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <android.support.v7.widget.RecyclerView android:id="@+id/recyclerView" android:layout_width="match_parent" android:layout_height="match_parent" android:scrollbars="horizontal" /> <com.bartoszlipinski.recyclerviewheader.RecyclerViewHeader android:id="@+id/header" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center_horizontal|top"> <LinearLayout android:layout_width="match_parent" android:orientation="vertical" android:layout_height="wrap_content"> <RelativeLayout android:id="@+id/as" android:layout_width="match_parent" android:layout_height="90dp" android:background="@color/white" > <ImageView android:id="@+id/imageView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="76dp" android:layout_marginTop="20dp" android:layout_centerVertical="true" android:src="@mipmap/service1" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_toRightOf="@+id/imageView1" android:gravity="center_vertical" android:orientation="vertical" > <TextView android:id="@+id/aa" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="12dp" android:text="@string/phone" android:textColor="#010101" android:textSize="17sp" /> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="12dp" android:text="@string/time" android:textColor="#606060" android:textSize="12sp" /> </LinearLayout> </RelativeLayout> <View style="@style/lines_dbdbdb" /> </LinearLayout> </com.bartoszlipinski.recyclerviewheader.RecyclerViewHeader> </FrameLayout> </LinearLayout>
compile 'com.android.support:recyclerview-v7:23.1.1' compile 'com.android.support:appcompat-v7:23.1.1' compile 'com.android.support:design:23.1.1' compile 'com.jakewharton:butterknife:7.0.1' compile 'com.bartoszlipinski.recyclerviewheader:library:1.2.0' 加入header compile 'com.squareup.retrofit2:adapter-rxjava:2.0.1' compile 'com.squareup.retrofit2:retrofit:2.0.1' compile 'com.squareup.retrofit2:converter-gson:2.0.1' compile 'com.google.code.gson:gson:2.6.2' compile 'com.facebook.fresco:fresco:0.9.0+' compile 'io.reactivex:rxjava:1.1.2' compile 'io.reactivex:rxandroid:1.1.0' compile files('libs/fraudmetrix-2.0.5.jar') compile files('libs/locSDK_6.03.jar')