淘忆项目之主页界面客户端的整理归纳
来源:互联网 发布:sql 找不到指定的路径 编辑:程序博客网 时间:2024/05/11 16:05
淘忆项目之主页界面客户端的整理归纳
主页主要包括两个部分,第一部分是轮播图,第二部分是全部物件的简略展示。
轮播图的实现。
首先是用photoshop设计界面:
先在build.gradle中添加
compile 'com.jude:rollviewpager:1.2.9'
然后重新加载一下内容。
第一步:建立一个新的Fragment。
首先添加一个类方法,命名为HomeRollShowAdapter,添加资源图片。
package com.elainetaylor.blog.adapter;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import com.elainetaylor.blog.R;
import com.jude.rollviewpager.adapter.StaticPagerAdapter;
/**
* Created by Eaine Taylor on 2016/7/15.
*/
public class HomeRollShowAdapterextends StaticPagerAdapter {
private int[]imgs={R.mipmap.roll_huaibiao,R.mipmap.roll_qiche,R.mipmap.roll_shuben,R.mipmap.roll_xinagji};
private Contextcontext;
public HomeRollShowAdapter(Context context){
this.context=context;
}
@Override
publicView getView(ViewGroup container, intposition) {
ImageView view=newImageView(container.getContext());
view.setImageResource(imgs[position]);
view.setScaleType(ImageView.ScaleType.CENTER_CROP);
view.setLayoutParams(newViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT));
return view;
}
@Override
public intgetCount() {
returnimgs.length;
}
}
第二步:首先,准备连接服务器的网址。
public staticString getHomeItemURL() {
returnURL +"/servlet/HomeItemAction";
}
第三步:根据图片编写布局文件:
<LinearLayoutxmlns: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:background="#f8f8f8"
android:orientation="vertical"
tools:context="com.elainetaylor.blog.ui.fragment.HomeFragment">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#fff">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="12dp"
android:gravity="center"
android:text="首页"
android:textSize="20sp"/>
</LinearLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<com.jude.rollviewpager.RollPagerView
android:id="@+id/rp_home"
android:layout_width="match_parent"
android:layout_height="180dp"
android:layout_gravity="center">
</com.jude.rollviewpager.RollPagerView>
<View
android:layout_width="match_parent"
android:layout_height="4dp"
android:background="#f8f8f8"/>
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/rv_home"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
</ScrollView>
</LinearLayout>
第四步:编写item_home.xml文件的内容,作为recyclerView的每一个条目
<?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:background="#fff"
android:orientation="vertical">
<View
android:layout_width="match_parent"
android:layout_height="8dp"
android:background="#f8f8f8"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:orientation="horizontal">
<ImageView
android:id="@+id/niv_Head"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
android:background="@drawable/ib_user_head"
android:scaleType="fitXY"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
android:orientation="vertical">
<TextView
android:clickable="true"
android:id="@+id/tv_Name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:layout_marginBottom="5dp"
android:layout_marginTop="3dp"
android:text="ElaineTaylor"
android:textColor="#707070"
android:textSize="13sp"/>
<TextView
android:id="@+id/tv_Time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2016年07月11日"
android:textColor="#4000"
android:textSize="12sp"/>
</LinearLayout>
</LinearLayout>
<ImageView
android:id="@+id/niv_ContentPic"
android:layout_width="match_parent"
android:layout_height="250dp"
android:layout_marginTop="10dp"
android:scaleType="fitXY"/>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:padding="3dp">
<LinearLayout
android:id="@+id/linearLayout2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:orientation="horizontal">
<ImageView
android:id="@+id/iv_save"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/icon_star"/>
<TextView
android:id="@+id/tv_SaveNum"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:layout_marginRight="2dp"
android:layout_marginEnd="2dp"/>
<ImageView
android:id="@+id/iv_Mes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/icon_message"/>
<TextView
android:id="@+id/tv_MesNum"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:layout_marginRight="2dp"
android:layout_marginEnd="2dp"/>
</LinearLayout>
</FrameLayout>
</LinearLayout>
第五步:然后是编写HomeItemAdapter的内容。
package com.elainetaylor.blog.adapter;
import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.elainetaylor.blog.R;
import com.elainetaylor.blog.listener.OnItemClickListener;
import com.elainetaylor.blog.ui.activity.HimActivity;
import com.nostra13.universalimageloader.core.ImageLoader;
import java.util.List;
/**
* Created by Eaine Taylor on 2016/8/11.
*/
public class HomeItemAdapterextends RecyclerView.Adapter<HomeItemAdapter.HomeItemViewHolder> {
privateList<String> username;
private List<String>submitTime;
private List<String>headPic;
private List<String>contentPic;
private List<Integer>thingSaveNum;
private List<Integer>thingMesNum;
private Contextcontext;
private OnItemClickListenerlistener;
public HomeItemAdapter(List<String> username,List<String> submitTime,List<String> headPic,List<String> contentPic,List<Integer> thingSaveNum,List<Integer> thingMesNum,Context context) {
this.username= username;
this.submitTime= submitTime;
this.headPic= headPic;
this.contentPic= contentPic;
this.thingSaveNum= thingSaveNum;
this.thingMesNum= thingMesNum;
this.context= context;
}
public voidsetOnItemClickListener(OnItemClickListener listener) {
this.listener= listener;
}
@Override
publicHomeItemViewHolder onCreateViewHolder(ViewGroup parent, intviewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_home,parent, false);
HomeItemViewHolder holder =new HomeItemViewHolder(view,listener);
return holder;
}
@Override
public voidonBindViewHolder(HomeItemViewHolder holder, intposition) {
holder.tvUsername.setText(username.get(position));
holder.tvSubmitTime.setText(submitTime.get(position));
holder.tvSaveNum.setText(thingSaveNum.get(position).toString());
holder.tvMesNum.setText(thingMesNum.get(position).toString());
ImageLoader loader=ImageLoader.getInstance();
loader.displayImage(contentPic.get(position),holder.nivContentPic);
loader.displayImage(headPic.get(position),holder.nivHeadPic);
}
@Override
public intgetItemCount() {
returnusername.size();
}
classHomeItemViewHolder extendsRecyclerView.ViewHolder implementsView.OnClickListener {
publicTextView tvUsername,tvSubmitTime,tvSaveNum,tvMesNum;
public ImageViewnivHeadPic,nivContentPic;
public OnItemClickListenerlistener;
public HomeItemViewHolder(View itemView,OnItemClickListener listener) {
super(itemView);
tvUsername= (TextView) itemView.findViewById(R.id.tv_Name);
tvSubmitTime= (TextView) itemView.findViewById(R.id.tv_Time);
tvSaveNum= (TextView) itemView.findViewById(R.id.tv_SaveNum);
tvMesNum= (TextView) itemView.findViewById(R.id.tv_MesNum);
nivHeadPic= (ImageView) itemView.findViewById(R.id.niv_Head);
nivContentPic= (ImageView) itemView.findViewById(R.id.niv_ContentPic);
this.listener= listener;
itemView.setOnClickListener(this);
nivHeadPic.setOnClickListener(this);
nivContentPic.setOnClickListener(this);
}
@Override
public voidonClick(View view) {
switch(view.getId()){
caseR.id.niv_Head://当用户点击头像的时候跳转到用户信息的界面
Intent intent=newIntent(context,HimActivity.class);
context.startActivity(intent);
break;
case R.id.niv_ContentPic://当用户点击图片的时候跳转到详情页
if(listener!= null) {
listener.OnclickItemClick(view,getPosition());
}
break;
}
}
}
}
第六步:对HomeFragment的编写,这里的Fragment继承自BaseFragment,BaseFragment的内容和BaseActivity的内容差不多。
package com.elainetaylor.blog.ui.fragment;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
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.Toast;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.elainetaylor.blog.R;
import com.elainetaylor.blog.adapter.HomeItemAdapter;
import com.elainetaylor.blog.adapter.HomeRollShowAdapter;
import com.elainetaylor.blog.common.BaseFragment;
import com.elainetaylor.blog.common.UrlUtils;
import com.elainetaylor.blog.listener.OnItemClickListener;
import com.elainetaylor.blog.ui.activity.DetailActivity;
import com.jude.rollviewpager.RollPagerView;
import com.jude.rollviewpager.hintview.ColorPointHintView;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
public class HomeFragmentextends BaseFragmentimplements OnItemClickListener {
privateRollPagerView rpHome;
private RecyclerViewrvHome;
private List<String>nameList;
private List<String>timeList;
private List<Integer>saveNumList;
private List<Integer>mesNumList;
private List<String>headUrlList;
private List<String>contentUrlList;
private List<String>thingIdList;
private HomeItemAdapter adapter;
@Override
public voidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
publicView onCreateView(LayoutInflater inflater,ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_home,container, false);
rpHome= (RollPagerView) view.findViewById(R.id.rp_home);
rvHome= (RecyclerView) view.findViewById(R.id.rv_home);
initRequestQueue();
initRollPagerView();
initData();
getData();
return view;
}
public voidinitRollPagerView() {
rpHome.setPlayDelay(2000);
rpHome.setAnimationDurtion(500);
rpHome.setAdapter(newHomeRollShowAdapter(getActivity()));
rpHome.setHintView(newColorPointHintView(getActivity(),Color.BLACK,Color.WHITE));
}
public voidinitData() {
nameList= newArrayList<String>();
timeList= newArrayList<String>();
headUrlList= newArrayList<String>();
contentUrlList= newArrayList<String>();
mesNumList= newArrayList<Integer>();
saveNumList= newArrayList<Integer>();
thingIdList= newArrayList<>();
}
public voidgetData() {
StringRequest request = new StringRequest(Request.Method.POST,UrlUtils.getHomeItemURL(), newResponse.Listener<String>() {
@Override
public voidonResponse(String s) {
try{
JSONObject object1 =new JSONObject(s);
JSONArray array = object1.getJSONArray("thing");
for (inti = array.length() - 1;i >= 0;i--) {
JSONObject object = array.getJSONObject(i);
contentUrlList.add(UrlUtils.getPicUrl() + object.getString("thingPic"));
timeList.add(object.getString("submitTime"));
mesNumList.add(object.getInt("thingMesNum"));
saveNumList.add(object.getInt("thingGoodNum"));
thingIdList.add(object.getString("thingId"));
nameList.add(object.getString("username"));
headUrlList.add(UrlUtils.getPicUrl() + object.getString("userHead"));
}
LinearLayoutManager manager =new LinearLayoutManager(getActivity());
rvHome.setLayoutManager(manager);
adapter= newHomeItemAdapter(nameList,timeList,headUrlList,contentUrlList,saveNumList,mesNumList,getActivity());
rvHome.setAdapter(adapter);
adapter.setOnItemClickListener(HomeFragment.this);
}catch (JSONException e) {
e.printStackTrace();
}
}
}, newResponse.ErrorListener() {
@Override
public voidonErrorResponse(VolleyError volleyError) {
Toast.makeText(getActivity(),"服务器出错",Toast.LENGTH_SHORT).show();
}
});
addRequestQueue(request);
}
//将thingId传递给详情页
@Override
public voidOnclickItemClick(View view, intpositon) {
Intent intent = newIntent(getActivity(),DetailActivity.class);
intent.putExtra("thingId",thingIdList.get(positon));
startActivity(intent);
}
}
- 淘忆项目之主页界面客户端的整理归纳
- 淘忆项目之主页界面服务器端的修正归纳
- 淘忆项目之分享界面客户端的修正归纳
- 淘忆项目之欢迎界面的归纳分享
- 淘忆项目之引导界面的分享归纳
- 淘忆项目之注册界面的修正归纳
- 淘忆项目之登录界面的修正归纳
- 淘忆项目之忘记密码界面的修正归纳
- 淘忆项目之主界面的修正归纳
- 淘忆项目之主题界面的修正归纳
- 淘忆项目之主题详情界面的修正归纳
- 淘忆项目之分享界面服务器端的修正归纳
- 安卓项目之淘忆2.0的代码实现之主页底部的导航栏功能
- 使用开源组件slidingmenu_library来模拟实现人人客户端的主页侧滑界面
- 使用开源组件slidingmenu_library来模拟实现人人客户端的主页侧滑界面
- 游戏客户端之cocostudio的界面库
- java之IO流归纳整理
- 软件工程之软件测试归纳整理
- 摄像头驱动笔记1---V4L2框架分析
- SSH系列(二):Spring + Hibernate + SpringMVC Maven整合
- Python 读取csv的某行
- SVN客户端安装和服务器搭建
- Html最详细语法
- 淘忆项目之主页界面客户端的整理归纳
- SSH项目环境搭建
- 了解build.gradle
- Leetcode no. 98
- mysql logstash 配置
- POJ2686 状态压缩
- 如何选择 compileSdkVersion, minSdkVersion 和 targetSdkVersion
- Asp.net文本框输入提示功能(jquery-autocomplete)
- cordova环境配置