我将此布局扩展到Myadapter类。
问题是recyclerview项目是从下到上加载的。
如果我将layout_gravity设置为top,它会隐藏在cardview下面。 所以我把它设置为中心,但正如我所说它继续从底部加载到顶部。
我应该做的是:
1 - 在我的cardview下设置我的recyclerview。
2 - 像往常一样(从上到下)在recyclerview中装载物品
3-如何为Recyclerview的每一行项目着色?
像第一行绿色第二行黄色并重复它。
Layout.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/coordinatorLayout"
xmlns:card_view="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<android.support.v7.widget.CardView
android:id="@+id/card_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardCornerRadius="5dp"
app:cardUseCompatPadding="true"
card_view:cardCornerRadius="4dp">
<FrameLayout
android:id="@+id/frame_color"
android:layout_width="6dp"
android:layout_height="match_parent"/>
<RelativeLayout
android:id="@+id/rel"
android:padding="20dp"
android:layoutDirection="ltr"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/versus_six"
android:text="@string/X"
android:textSize="16sp"
android:layout_centerInParent="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
>
<ImageView
android:id="@+id/image_team1"
android:layout_width="60dp"
android:layout_height="60dp"
android:adjustViewBounds="true"
android:layout_gravity="start"
android:src="@drawable/placemahdi"
android:scaleType="fitXY"
/>
<TextView
android:id="@+id/name_team1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15sp"
android:layout_gravity="center"
android:textAlignment="center"/>
</LinearLayout>
<LinearLayout
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end">
<ImageView
android:id="@+id/image_team2"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_gravity="end"
android:src="@drawable/placemahdi"
android:adjustViewBounds="true"
/>
<TextView
android:id="@+id/name_team2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textAlignment="center"
android:textSize="15sp" />
</LinearLayout>
<TextView
android:textAlignment="center"
android:layout_gravity="start"
android:id="@+id/txt_stadium"
android:text="ملعب المرداشية"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textColor="#666666"
android:textSize="12sp" />
</RelativeLayout>
</android.support.v7.widget.CardView>
<RelativeLayout
android:id="@+id/re2"
android:layoutDirection="ltr"
android:layout_gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/swipe_refresh"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<android.support.v7.widget.RecyclerView
android:layout_gravity="center"
android:id="@+id/recycler_statistics"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</android.support.v4.widget.NestedScrollView>
</android.support.v4.widget.SwipeRefreshLayout>
</RelativeLayout>
</android.support.design.widget.CoordinatorLayout>
更新:
我的适配器类:
public class PostAdapter5 extends RecyclerView.Adapter<PostAdapter5.ViewHolder>{
public Context mContext;
public ArrayList<Goal_data> original_items = new ArrayList<>();
public ArrayList<Goal_data> filtered_items = new ArrayList<>();
// ItemFilter mFilters = new ItemFilter();
public PostAdapter5(Context mContext, ArrayList<Goal_data> postList) {
this.mContext = mContext;
this.original_items = postList;
this.filtered_items = postList;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_statistics, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
try {
final Goal_data post = filtered_items.get(position);
// holder.frameLayout.setBackgroundColor((mContext.getResources().getColor(R.color.colorAccent)));
holder.goaler.setText(post.getName_goaler());
holder.recent.setText(post.getTime_goal()+"'");
holder.point1.setText(post.getPoint1());
holder.point2.setText(post.getPoint2());
// holder.point_team1.setText(post.getScore1());
// holder.point_team2.setText(post.getScore2());
// PicassoClient.downloadImage(mContext, post.getImage_nationality(), holder.image_nationality);
// PicassoClient.downloadImage(mContext, post.getPlayer_image(), holder.image_player);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public int getItemCount() {
return filtered_items.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
private TextView point1,point2,point_team1,point_team2,goaler,recent;
private ImageView image_player,image_nationality;
private FrameLayout frameLayout;
public ViewHolder(View itemView) {
super(itemView);
// point1=(TextView)itemView.findViewById(R.id.point1);
// point2=(TextView)itemView.findViewById(R.id.point2);
// point_team1=(TextView) itemView.findViewById(R.id.point_team1);
// point_team2=(TextView) itemView.findViewById(R.id.point_team2);
goaler = (TextView) itemView.findViewById(R.id.txt_goaler);
recent = (TextView) itemView.findViewById(R.id.recent);
point1 = (TextView) itemView.findViewById(R.id.point_team1);
point2 = (TextView) itemView.findViewById(R.id.point_team2);
}
}
}
我的片段代码:
public class SixFragment extends Fragment implements ConnectivityReceiver.ConnectivityReceiverListener,
SwipeRefreshLayout.OnRefreshListener{
public CoordinatorLayout coordinatorLayout;
public boolean isConnected;
public RecyclerView recycler_statistics;
public PostAdapter5 adapter;
public ProgressDialog progressDialog;
ArrayList<Goal_data> post_array = new ArrayList<>();
public SwipeRefreshLayout swipeRefreshLayout;
public String id,st_image1,st_image2,st_team1,st_team2,stadium,tour;
ImageView image1,image2;
public TextView name_team1,name_team2;
private FrameLayout frameLayout;
@Override
public void onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
}
public SixFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
checkConnectivity();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v=inflater.inflate(R.layout.fragment_six,null);
coordinatorLayout = (CoordinatorLayout) v.findViewById(R.id.coordinatorLayout);
recycler_statistics = (RecyclerView) v.findViewById(R.id.recycler_statistics);
LinearLayoutManager layoutManager = new LinearLayoutManager(this.getActivity(),LinearLayoutManager.VERTICAL,false);
recycler_statistics.setLayoutManager(layoutManager);
recycler_statistics.setItemAnimator(new DefaultItemAnimator());
swipeRefreshLayout = (SwipeRefreshLayout) v.findViewById(R.id.swipe_refresh);
swipeRefreshLayout.setOnRefreshListener(this);
swipeRefreshLayout.setColorSchemeResources(R.color.colorRed,R.color.colorGreen,R.color.black);
SharedPreferences sharedPreferences = getContext().getSharedPreferences(getString(R.string.PREF_FILE), MODE_PRIVATE);
id = sharedPreferences.getString(getString(R.string.ID), "");
stadium = sharedPreferences.getString(getString(R.string.stadium), "");
tour = sharedPreferences.getString(getString(R.string.tour), "");
st_image1 = sharedPreferences.getString(getString(R.string.TEAMNEW1_image_1), "");
st_image2 = sharedPreferences.getString(getString(R.string.TEAMNEW2_image_2), "");
st_team1 = sharedPreferences.getString(getString(R.string.TEAMNEW1_name), "");
st_team2 = sharedPreferences.getString(getString(R.string.TEAMNEW2_name), "");
image1=(ImageView)v.findViewById(R.id.image_team1);
image2=(ImageView)v.findViewById(R.id.image_team2);
frameLayout=(FrameLayout)v.findViewById(R.id.frame_color);
frameLayout.setBackgroundColor((getContext().getResources().getColor(R.color.newgreen)));
PicassoClient.downloadImage(getContext(),st_image1,image1);
PicassoClient.downloadImage(getContext(),st_image2,image2);
name_team1=(TextView)v.findViewById(R.id.name_team1);
name_team2=(TextView)v.findViewById(R.id.name_team2);
name_team1.setText(st_team1);
name_team2.setText(st_team2);
try {
getData();
} catch (Exception e) {
e.printStackTrace();
}
return v;
}
public void getData() throws Exception {
if (checkConnectivity()){
try {
swipeRefreshLayout.setRefreshing(true);
getAllPosts();
} catch (Exception e) {
e.printStackTrace();
}
}else {
swipeRefreshLayout.setRefreshing(false);
// getAllPosts();
showSnack();
}
}
public boolean checkConnectivity() {
return ConnectivityReceiver.isConnected();
}
public void showSnack() {
Snackbar.make(coordinatorLayout, getString(R.string.no_internet_connected), Snackbar.LENGTH_INDEFINITE)
.setAction(getString(R.string.settings), new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent(Settings.ACTION_WIRELESS_SETTINGS));
}
}).setActionTextColor(Color.RED)
.show();
}
@Override
public void onResume() {
super.onResume();
// Toast.makeText(getContext(),"u have resumed the app",Toast.LENGTH_SHORT).show();
AppController.getInstance().setConnectivityReceiver(this);
}
@Override
public void onRefresh() {
try {
// Toast.makeText(getContext(),"u have refreshed the app",Toast.LENGTH_SHORT).show();
//when u swipe the app..the getdata method is invoked !
getData();
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onPause() {
super.onPause();
// Toast.makeText(getContext(),"u have paused the app",Toast.LENGTH_SHORT).show();
}
@Override
public void onNetworkChange(boolean inConnected) {
this.isConnected = inConnected;
// Toast.makeText(getContext(),"the app network have been changed",Toast.LENGTH_SHORT).show();
}
public void getAllPosts() throws Exception{
String TAG = "STATISTICS";
String url = Constants.STATISTICS_URL;
StringRequest jsonObjectRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.i("response", response);
parseJson(response);
progressDialog.dismiss();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
try {
swipeRefreshLayout.setRefreshing(false);
progressDialog.dismiss();
Log.e("error", "" +error.getMessage());
}catch (NullPointerException e)
{
swipeRefreshLayout.setRefreshing(false);
e.printStackTrace();
}
}
})
{
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String,String> params= new HashMap<String,String>();
params.put("id",id);
return params;
}
};
AppController.getInstance().addToRequestQueue(jsonObjectRequest, TAG);
progressDialog = new ProgressDialog(getActivity());
progressDialog.setMessage("الرجاء الانتظار..");
progressDialog.show();
}
public void parseJson(String response){
try {
JSONArray array = new JSONArray(response);
JSONObject jsonObject =null;
post_array.clear();
Goal_data p;
for(int i=0 ; i<array.length() ; i++)
{
jsonObject=array.getJSONObject(i);
String id_plus= jsonObject.getString("id_plus");
String id_goal=jsonObject.getString("id_goal");
String name_goaler=jsonObject.getString("name_goaler");
String time_goal=jsonObject.getString("time_goal");
String point1=jsonObject.getString("point1");
String point2=jsonObject.getString("point2");
p = new Goal_data();
p.setId_plus(id_plus);
p.setId_goal(id_goal);
p.setName_goaler(name_goaler);
p.setPoint1(point1);
p.setPoint2(point2);
p.setTime_goal(time_goal);
post_array.add(p);
//realmHelper.save(p);
}
}
catch (JSONException e) {
swipeRefreshLayout.setRefreshing(false);
e.printStackTrace();
//Log.d("error", e.getMessage());
}
adapter = new PostAdapter5(getContext(), post_array);
recycler_statistics.setAdapter(adapter);
swipeRefreshLayout.setRefreshing(false);
}
}
更新3:
包含项目的布局必须显示在recyclerview中 (上面的布局是一个静态布局,用于显示团队的图像和名称)
<?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"
xmlns:card_view="http://schemas.android.com/tools"
android:id="@+id/coordinatorLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.CardView
android:id="@+id/card_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardCornerRadius="5dp"
app:cardUseCompatPadding="true"
card_view:cardCornerRadius="4dp">
<RelativeLayout
android:layoutDirection="ltr"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/image_news"
android:layout_marginRight="5dp"
android:layout_marginEnd="5dp"
android:layout_width="20dp"
android:layout_height="20dp"
android:adjustViewBounds="true"
android:background="@drawable/ic_football"
android:layout_alignParentLeft="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:scaleType="fitXY" />
<TextView
android:id="@+id/txt_goaler"
android:layout_width="wrap_content"
android:layout_gravity="start"
android:text="أحمد زريق"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toLeftOf="@+id/image_news"
android:layout_marginRight="3dp"
android:layout_marginEnd="3dp"
android:textSize="16sp" />
<TextView
android:id="@+id/versus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/dash"
android:textSize="25sp" />
<TextView
android:id="@+id/point_team1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_marginRight="5dp"
android:layout_marginEnd="5dp"
android:layout_toStartOf="@+id/versus"
android:layout_toLeftOf="@+id/versus"
android:textSize="15sp" />
<TextView
android:id="@+id/point_team2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_marginLeft="5dp"
android:layout_marginStart="5dp"
android:layout_toRightOf="@+id/versus"
android:layout_toEndOf="@+id/versus"
android:textSize="15sp" />
<View
android:id="@+id/view1"
android:layout_below="@+id/image_news"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginTop="10dp" />
<TextView
android:id="@+id/recent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:text="'90'"
android:textSize="15sp"
android:layout_marginStart="5dp"
android:layout_marginLeft="5dp"
android:layout_gravity="center"
android:layout_marginRight="2dp"
android:textAlignment="center" />
</RelativeLayout>
</android.support.v7.widget.CardView>
</android.support.design.widget.CoordinatorLayout>
最终更新: BEFORE
AFTER
答案 0 :(得分:0)
我的猜测是您使用了以下构造函数的LinearLayoutManager
:
LinearLayoutManager(Context context, int orientation, boolean reverseLayout)
使用
reverseLayout = true
可以完美地解释你的问题。
答案 1 :(得分:0)
您尝试过setReverseLayout(true)
还是setStackFromEnd(true)
还是两者都尝试过?