从零开始做项目快完结啦,到处找零件拼成自己想要的啦~先上模型
用鼠标操作模拟器的,有点那个哈哈哈~手机上就不会啦
话不多说上代码了,SwipeRefreshLayout+RecyclerView是参考第一行代码书里的,再结合TablayoutView+Fragement+ViewPaper打造一个类似今日头条的的布局
MainActivity
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends FragmentActivity {
private TabLayout tabLayout_shouye;
private ViewPager viewPager_shouye;
private List<String> strings = new ArrayList<String>();;
private List<Fragment> fragments = new ArrayList<Fragment>();;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initdate();
initView();
}
private void initView(){
tabLayout_shouye = (TabLayout)findViewById(R.id.tablayout_shouye);
viewPager_shouye = (ViewPager)findViewById(R.id.viewpager_ShouYe);
viewPager_shouye.setAdapter(new TabFragmentShouYeAdapter(fragments,strings,
getSupportFragmentManager(),this));
tabLayout_shouye.setupWithViewPager(viewPager_shouye);
tabLayout_shouye.setTabTextColors(getResources().getColor(R.color.colorPrimary)//未选中颜色
,getResources().getColor(R.color.colorAccent));//被选颜色
}
private void initdate(){
Fragment1 fragment1 = new Fragment1();
fragments.add(fragment1);
strings.add(" 第一个 ");
Fragment2 fragment2 = new Fragment2();
fragments.add(fragment2);
strings.add(" 第二个 ");
Fragment3 fragment3 = new Fragment3();
fragments.add(fragment3);
strings.add(" 第三个 ");
Fragment4 fragment4 = new Fragment4();
fragments.add(fragment4);
strings.add(" 第四个");
}
}
给activity价格适配器
package example.hafd.com.tablayoutviewpagertest;
import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import java.util.List;
public class TabFragmentShouYeAdapter extends FragmentPagerAdapter {
private Context context;
private List<Fragment> fragments;
private List<String> strings;
public TabFragmentShouYeAdapter(List<Fragment> fragments, List<String> strings, FragmentManager fragmentManager, Context context){
super(fragmentManager);
this.strings = strings;
this.context = context;
this.fragments = fragments;
}
@Override
public Fragment getItem(int position) {
return fragments.get(position);
}
@Override
public int getCount() {
return strings.size();
}
@Override
public CharSequence getPageTitle(int position) {
return strings.get(position);
}
}
对应布局
<?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"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/gray5">
<TextView
android:layout_width="match_parent"
android:layout_height="45dp"
android:gravity="center"
android:textSize="17sp"
android:background="@color/white"
android:text="我的订单"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="35dp"
android:orientation="horizontal"
android:gravity="center_vertical"
android:layout_marginTop="5dp"
android:background="#eeeeee">
<!--滑动下划线的颜色-->
<android.support.design.widget.TabLayout
android:id="@+id/tablayout_shouye"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@color/white"
app:tabMode="scrollable"
app:tabIndicatorHeight="2dp"
app:tabIndicatorColor="@color/colorAccent">
</android.support.design.widget.TabLayout>
</LinearLayout>
<android.support.v4.view.ViewPager
android:id="@+id/viewpager_ShouYe"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
</android.support.v4.view.ViewPager>
</LinearLayout>
上一个Fragement就好了
package example.hafd.com.tablayoutviewpagertest;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class Fragment1 extends Fragment {
private View rootView;
private Fruit[] fruits = {new Fruit("Apple", 2), new Fruit("Banana", 2),
new Fruit("Orange", 3), new Fruit("Watermelon", 4),new Fruit("Watermelon", 4),
new Fruit("hellword", 5), new Fruit("Error", 6),new Fruit("Water", 7),
new Fruit("GUci", 8),new Fruit("Lol", 9),};
private List<Fruit> fruitList = new ArrayList<>();
private FruitAdapter adapter;
protected static final int CHANGE_UI = 1;
protected static final int ERROR = 2;
protected static final int FALSE = 3;
protected static final String TAG = "Fragment1";
private RecyclerView recyclerView;
private GridLayoutManager layoutManager;
private SwipeRefreshLayout swipeRefresh;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
rootView = (View) inflater.inflate(R.layout.fragement1, container, false);
Log.i("Fragment2", "hahaFragment1打开了" + 1);
recyclerView = rootView.findViewById(R.id.recycler_view);
//第二个参数设置几列
layoutManager = new GridLayoutManager(MyApplication.getContext(), 1);
initFruits();
setOrderAdapter();
// initDate();
swipeRefresh = rootView.findViewById(R.id.swipe_refresh);
// 设置下拉进度的主题颜色
swipeRefresh.setColorSchemeResources(android.R.color.holo_blue_bright, android.R.color.holo_green_light,
android.R.color.holo_orange_light, android.R.color.holo_red_light);
swipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
refreshFruits();
}
});
return rootView;
}
public void setOrderAdapter() {
recyclerView.setLayoutManager(layoutManager);
adapter = new FruitAdapter(fruitList);
recyclerView.setAdapter(adapter);
Log.i(TAG,"haha"+fruitList);
}
public void initFruits() {
fruitList.clear();
for (int i = 0; i < 5; i++) {
Random random = new Random();
int index = random.nextInt(fruits.length);
fruitList.add(fruits[index]);
}
}
private void refreshFruits() {
new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
initFruits();
Toast.makeText(MyApplication.getContext(), "下拉刷新", Toast.LENGTH_SHORT).show();
adapter.notifyDataSetChanged();
swipeRefresh.setRefreshing(false);
}
});
}
}).start();
}
/* public void initDate(){
new Thread() {
@Override
public void run() {
// int num=queryOrderById();
if (num > 0) {
Message msg = new Message();
msg.what = CHANGE_UI;
handler.sendMessage(msg);
} else {
Message msg = new Message();
msg.what = ERROR;
handler.sendMessage(msg);
}
}
}.start();
}*/
}
片段中的MyApplication.getContext()是自定义上下文的,附上好了
package example.hafd.com.tablayoutviewpagertest;
import android.app.Application;
import android.content.Context;
public class MyApplication extends Application {
private static Context context;
@Override
public void onCreate() {
super.onCreate();
context = getApplicationContext();
}
public static Context getContext() {
return context;
}
}
还需要再AndroidManifest.xml里声明:
<application
android:name=".MyApplication"
android:allowBackup="true"
fragement1.xml 注:此处要加LinerLayout,不然RecyclerView列表显示数据时布局会出现高度越界导致顶部显示部分被遮挡,或者自己写上计算高度的代码调整亦可,我懒就直接LinerLayout了
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/gray5"
android:orientation="vertical">
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<!--SwipeRefreshLayout下拉刷新View-->
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/swipe_refresh"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/navigation"
android:layout_marginTop="5dp"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.v4.widget.SwipeRefreshLayout>
</android.support.design.widget.CoordinatorLayout>
</LinearLayout>
好了,给RecyclerView加item布局格式,用CardView,记得添加依赖
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support:design:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.2'
implementation 'com.android.support:cardview-v7:26.1.0'
来个RecyclerView 的item
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
app:cardCornerRadius="5dp">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/fruit_image"
android:layout_width="match_parent"
android:layout_height="100dp"
android:scaleType="centerCrop" />
<TextView
android:id="@+id/fruit_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_margin="5dp"
android:textSize="16sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingBottom="2dp"
android:paddingTop="2dp" >
<TextView
android:id="@+id/S_address"
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="嘿嘿嘿"
android:singleLine="true"
android:textSize="16sp" />
<TextView
android:id="@+id/tv_Sname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_weight="1.5"
android:singleLine="true"
android:text="啦啦啦"
android:textSize="16sp" />
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
上数据 Fruit类
package example.hafd.com.tablayoutviewpagertest;
public class Fruit {
private String name;
private int imageId;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getImageId() {
return imageId;
}
public void setImageId(int imageId) {
this.imageId = imageId;
}
@Override
public String toString() {
return "Fruit{" +
"name='" + name + '\'' +
", imageId=" + imageId +
'}';
}
public Fruit(String name, int imageId) {
this.name = name;
this.imageId = imageId;
}
}
绑定数据到列表的工具 RecyclerView.Adapter
package example.hafd.com.tablayoutviewpagertest;
import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.CardView;
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 java.util.List;
public class FruitAdapter extends RecyclerView.Adapter<FruitAdapter.ViewHolder>{
private static final String TAG = "FruitAdapter";
private Context mContext;
private List<Fruit> mFruitList;
static class ViewHolder extends RecyclerView.ViewHolder {
CardView cardView;
ImageView fruitImage;
TextView fruitName,S_address,tv_Sname;
public ViewHolder(View view) {
super(view);
cardView = (CardView) view;
fruitImage = view.findViewById(R.id.fruit_image);
fruitName = view.findViewById(R.id.fruit_name);
S_address = view.findViewById(R.id.S_address);
tv_Sname = view.findViewById(R.id.tv_Sname);
}
}
public FruitAdapter(List<Fruit> fruitList) {
mFruitList = fruitList;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (mContext == null) {
mContext = parent.getContext();
}
View view = LayoutInflater.from(mContext).inflate(R.layout.fruit_item, parent, false);
final ViewHolder holder = new ViewHolder(view);
holder.cardView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int position = holder.getAdapterPosition();
Fruit fruit = mFruitList.get(position);
Intent intent = new Intent(mContext, FruitActivity.class);
intent.putExtra(FruitActivity.FRUIT_NAME, fruit.getName());
intent.putExtra(FruitActivity.FRUIT_IMAGE_ID, fruit.getImageId());
mContext.startActivity(intent);
}
});
return holder;
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
Fruit fruit = mFruitList.get(position);
holder.S_address.setText(fruit.getName());
holder.fruitName.setText(fruit.getName());
holder.tv_Sname.setText(fruit.getName());
}
@Override
public int getItemCount() {
return mFruitList.size();
}
}
几乎搞定了,加上项目点击后的炫酷详情页
FruitActivity
package example.hafd.com.tablayoutviewpagertest;
import android.content.Intent;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.TextView;
public class FruitActivity extends AppCompatActivity {
public static final String FRUIT_NAME = "fruit_name";
public static final String FRUIT_IMAGE_ID = "fruit_image_id";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fruit);
Intent intent = getIntent();
String fruitName = intent.getStringExtra(FRUIT_NAME);
// int fruitImageId = intent.getIntExtra(FRUIT_IMAGE_ID, 0);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
CollapsingToolbarLayout collapsingToolbar = (CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar);
ImageView fruitImageView = (ImageView) findViewById(R.id.fruit_image_view);
TextView fruitContentText = (TextView) findViewById(R.id.fruit_content_text);
setSupportActionBar(toolbar);
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(true);
}
collapsingToolbar.setTitle(fruitName);
String fruitContent = generateFruitContent(fruitName);
fruitContentText.setText(fruitContent);
}
private String generateFruitContent(String fruitName) {
StringBuilder fruitContent = new StringBuilder();
for (int i = 0; i < 500; i++) {
fruitContent.append(fruitName);
}
return fruitContent.toString();
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
}
activity_fruit.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<android.support.design.widget.AppBarLayout
android:id="@+id/appBar"
android:background="@color/colorAccent"
android:layout_width="match_parent"
android:layout_height="250dp"
android:fitsSystemWindows="true">
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/collapsing_toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:fitsSystemWindows="true"
app:contentScrim="@color/blue0"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<ImageView
android:id="@+id/fruit_image_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:fitsSystemWindows="true"
app:layout_collapseMode="parallax" />
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="45dp"
app:layout_collapseMode="pin" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="15dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_marginTop="35dp"
app:cardCornerRadius="4dp">
<TextView
android:id="@+id/fruit_content_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp" />
</android.support.v7.widget.CardView>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>