---------------------------------------之前的一些知识点:----------------------------------------------------
修改空间的布局:
首先每个推荐之间是有间隙的,
注意,包含的结构是这样的:activity_main.xml->fragment_home.xml->recommend_item.xml->album_item.xml
所以每个推荐之间的间隙,只要在recommend_item 里面修改添加marginTop的数值就可以了,还有给每个推荐修改背景色为白色
还有为了修改间隙之间的颜色修改fragment_home的背景色为灰色的。就可以了
还有 显示 的 title,一般都是如果超出了范围了 就是使用限制了 。
设置当TextView中的文字超过TextView的容量时,用省略号代替
只需要下边的设置:
textview.setSingleLine();
textview.setEllipsiz(TextUtils.TruncateAt.valueOf("END"));
在xml中设置如下:
android:singleLine="true"
android:ellipsize="end"
android:singleLine="true"
android:ellipsize="end"
---------------------------------------之前的一些知识点:----------------------------------------------------
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
发现新奇:
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
由于RecyclerViewAdapter已经实现是了多布局的复用,那么现在要做的
1:首先做一个布局discovery_item.xml,这个里面 要绑定一个 discovery的一个变量。
2: 然后做一个discovery_columns.xml的布局文件,将上面的 discovery_item绑定到这个来。
3:将该布局添加到到HomeFfragment里面。
在写discovery_item.xml 的时候
注意 SimpleDraweeView 里面的设置:
app:placeholderImage="@mipmap/ic_launcher"
app:imageURI="@{ Uri.parse( discovery.coverPath ) }"//靠!!!imageURI的i是小写的。
这里的面的最顶层的LinearLayout 可以不写
include的时候需要,导入这个layout里面的变量呢?
bind:user="@{user}"
注意 图片 这里 app:imageURI="@{ Uri.parse( discovery.coverPath ?? ``) }"
++++++++++++++++++++++++++++
靠!!!imageURI的i是小写的。
++++++++++++++++++++++++++++
在discovery_item.xml的顶层Layout 里面添加:
android:layout_marginTop="2dp"
android:background="@android:color/white"
现在的discovery_item.xml
-------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<import type="android.net.Uri"/>
<variable name="discovery" type="com.kodulf.ximalayating.entities.DiscoveryEntity"/>
</data>
<LinearLayout android:layout_marginTop="2dp" android:background="@android:color/white" android:orientation="horizontal" >
<com.facebook.drawee.view.SimpleDraweeView android:layout_width="40dp" android:layout_height="40dp" app:placeholderImage="@mipmap/ic_launcher" app:viewAspectRatio="1" app:imageURI="@{ Uri.parse( discovery.coverPath ?? ``) }" >
</com.facebook.drawee.view.SimpleDraweeView>
<LinearLayout android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:orientation="vertical">
<TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@{ discovery.title }">
</TextView>
<TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@{ discovery.subtitle }">
</TextView>
</LinearLayout>
</LinearLayout>
</layout>
-------------------------------------------------------------
discovery_columns.xml
--------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:bind="http://schemas.android.com/tools">
<data>
<variable name="discoveryColumns" type="com.kodulf.ximalayating.entities.DiscoveryColumns"/>
</data>
<LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="wrap_content">
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content">
<TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" text="@{ discoveryColumns.title }"/>
</LinearLayout>
<LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="wrap_content">
<include layout="@layout/discovery_item" android:layout_height="wrap_content" android:layout_width="match_parent" bind:discovery="@{ discoveryColumns.list[0] }" />
<include layout="@layout/discovery_item" android:layout_height="wrap_content" android:layout_width="match_parent" bind:discovery="@{ discoveryColumns.list[1] }" />
<include layout="@layout/discovery_item" android:layout_height="wrap_content" android:layout_width="match_parent" bind:discovery="@{ discoveryColumns.list[2] }" />
<include layout="@layout/discovery_item" android:layout_height="wrap_content" android:layout_width="match_parent" bind:discovery="@{ discoveryColumns.list[3] }" />
</LinearLayout>
</LinearLayout>
</layout>
--------------------------------------------------------------
HomeFragment里面添加两句话:
map.put(DiscoveryColumns.class,new BindingAdapter.BindingTool(R.layout.discovery_columns,BR.discoveryColumns));
adapter.add(body.getDiscoveryColumns());
package com.kodulf.ximalayating.fragments;
import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.databinding.tool.reflection.Callable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import com.kodulf.ximalayating.BR;
import com.kodulf.ximalayating.R;
import com.kodulf.ximalayating.adapters.BindingAdapter;
import com.kodulf.ximalayating.databinding.FragmentHomeBinding;
import com.kodulf.ximalayating.entities.DiscoveryColumns;
import com.kodulf.ximalayating.entities.DiscoveryEntity;
import com.kodulf.ximalayating.entities.HomeEntity;
import com.kodulf.ximalayating.entities.RecommendEntity;
import com.kodulf.ximalayating.utils.NetworkUtil;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import retrofit2.Callback;
import retrofit2.Response;
/** * A simple {@link Fragment} subclass. */ public class HomeFragment extends Fragment implements Callback<HomeEntity> {
private BindingAdapter adapter;
public HomeFragment() {
// Required empty public constructor }
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
HashMap<Type,BindingAdapter.BindingTool> map = new HashMap<>();
map.put(RecommendEntity.class,new BindingAdapter.BindingTool(R.layout.recommend_item, BR.recommend));
map.put(DiscoveryColumns.class,new BindingAdapter.BindingTool(R.layout.discovery_columns,BR.discoveryColumns));
adapter = new BindingAdapter(map, new ArrayList<>(), getContext());
NetworkUtil.getService().getHome().enqueue(this);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment // return inflater.inflate(R.layout.fragment_home, container, false); //初始化的时候需要修改了,不要上面的inflater了。 FragmentHomeBinding binding = DataBindingUtil.inflate(inflater, R.layout.fragment_home, container, false);
binding.homeRecycler.setAdapter(adapter);
return binding.getRoot();
}
@Override
public void onResponse(Response<HomeEntity> response) {
// Toast.makeText(getContext(),"返回",Toast.LENGTH_LONG).show(); HomeEntity body = response.body();
adapter.add(body.getEditorRecommendAlbums());
adapter.addAll(body.getHotRecommends().getList());
adapter.add(body.getDiscoveryColumns());
}
@Override
public void onFailure(Throwable t) {
t.printStackTrace();
}
}