gpt4 book ai didi

android - Material 设计导航适配器页脚/页眉(如何添加页脚?)

转载 作者:太空狗 更新时间:2023-10-29 14:54:56 25 4
gpt4 key购买 nike

我有问题,我想在我的导航左侧放置一个页脚。

我有一个取决于代码的页眉,但我不知道如何添加页脚,我尝试了所有方法,但没有任何效果:

我读了这篇文章:Android 5.0 - Add header/footer to a RecyclerView ,但我的代码非常不同。

谢谢。

我把我的代码放在 XML 和 java 中:

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {

private static final int TYPE_HEADER = 0; // Declaring Variable to Understand which View is being worked on
// IF the viaew under inflation and population is header or Item
private static final int TYPE_ITEM = 1;

private String mNavTitles[]; // String Array to store the passed titles Value from MainActivity.java
private int mIcons[]; // Int Array to store the passed icons resource value from MainActivity.java

private String name; //String Resource for header View Name
private int profile; //int Resource for header view profile picture
private String email; //String Resource for header view email
Context context;


// Creating a ViewHolder which extends the RecyclerView View Holder
// ViewHolder are used to to store the inflated views in order to recycle them

public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
int Holderid;

TextView textView;
ImageView imageView;
ImageView profile;
TextView Name;
TextView email;
Context contxt;


public ViewHolder(View itemView,int ViewType,Context c) { // Creating ViewHolder Constructor with View and viewType As a parameter
super(itemView);
contxt = c;
itemView.setClickable(true);
itemView.setOnClickListener(this);
// Here we set the appropriate view in accordance with the the view type as passed when the holder object is created

if(ViewType == TYPE_ITEM) {
textView = (TextView) itemView.findViewById(R.id.rowText); // Creating TextView object with the id of textView from item_row.xml
imageView = (ImageView) itemView.findViewById(R.id.rowIcon);// Creating ImageView object with the id of ImageView from item_row.xml
Holderid = 1; // setting holder id as 1 as the object being populated are of type item row
}
else{


Name = (TextView) itemView.findViewById(R.id.name); // Creating Text View object from header.xml for name
email = (TextView) itemView.findViewById(R.id.email); // Creating Text View object from header.xml for email
profile = (ImageView) itemView.findViewById(R.id.circleView);// Creating Image view object from header.xml for profile pic
Holderid = 0; // Setting holder id = 0 as the object being populated are of type header view
}



}


@Override
public void onClick(View v) {

Toast.makeText(contxt,"The Item Clicked is: "+getPosition(),Toast.LENGTH_SHORT).show();

}
}



MyAdapter(String Titles[], int Icons[], String Name, String Email, int Profile, Context passedContext){ // MyAdapter Constructor with titles and icons parameter
// titles, icons, name, email, profile pic are passed from the main activity as we
mNavTitles = Titles; //have seen earlier
mIcons = Icons;
name = Name;
email = Email;
profile = Profile; //here we assign those passed values to the values we declared here
this.context = passedContext;

//in adapter



}



//Below first we ovverride the method onCreateViewHolder which is called when the ViewHolder is
//Created, In this method we inflate the item_row.xml layout if the viewType is Type_ITEM or else we inflate header.xml
// if the viewType is TYPE_HEADER
// and pass it to the view holder

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

if (viewType == TYPE_ITEM) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_row,parent,false); //Inflating the layout

ViewHolder vhItem = new ViewHolder(v,viewType,context); //Creating ViewHolder and passing the object of type view

return vhItem; // Returning the created object

//inflate your layout and pass it to view holder

} else if (viewType == TYPE_HEADER) {

View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.header,parent,false); //Inflating the layout

ViewHolder vhHeader = new ViewHolder(v,viewType,context); //Creating ViewHolder and passing the object of type view

return vhHeader; //returning the object created


}
return null;

}

//Next we override a method which is called when the item in a row is needed to be displayed, here the int position
// Tells us item at which position is being constructed to be displayed and the holder id of the holder object tell us
// which view type is being created 1 for item row
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
if(holder.Holderid ==1) { // as the list view is going to be called after the header view so we decrement the
// position by 1 and pass it to the holder while setting the text and image
holder.textView.setText(mNavTitles[position - 1]); // Setting the Text with the array of our Titles
holder.imageView.setImageResource(mIcons[position -1]);// Settimg the image with array of our icons
}
else{

holder.profile.setImageResource(profile); // Similarly we set the resources for header view
holder.Name.setText(name);
holder.email.setText(email);
}
}

// This method returns the number of items present in the list
@Override
public int getItemCount() {
return mNavTitles.length+1; // the number of items in the list will be +1 the titles including the header view.
}


// Witht the following method we check what type of view is being passed
@Override
public int getItemViewType(int position) {
if (isPositionHeader(position))
return TYPE_HEADER;

return TYPE_ITEM;
}

private boolean isPositionHeader(int position) {
return position == 0;
}

}

我的标题。

<FrameLayout 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" tools:context="com.onpocket.alcover.Fragment_0_6"
>

<!-- TODO: Update blank fragment layout -->

<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="178dp">

<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/contenedor_img_2_1_6_3"
android:id="@+id/l1">

<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.3" >

<de.hdodenhof.circleimageview.CircleImageView
android:layout_width="70dp"
android:layout_height="70dp"
android:src="@drawable/barra_lateral_img_0_1"
android:layout_marginLeft="16dp"
android:layout_marginTop="38dp"
android:id="@+id/circleView"
android:adjustViewBounds="false" />
</LinearLayout>

<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.9"
android:background="#af000000"
android:gravity="center_vertical"
android:focusableInTouchMode="false"
android:id="@+id/l3"
android:focusable="false">

<LinearLayout
android:orientation="horizontal"
android:layout_width="15dp"
android:layout_height="match_parent" />

<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ffffff"
android:text="text"
android:textSize="14sp"
android:textStyle="bold"
android:id="@+id/name"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="text"
android:id="@+id/email"
android:textColor="#ffffffff" />
</LinearLayout>

</LinearLayout>

</LinearLayout>

</LinearLayout>
</FrameLayout>


<!--
<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="178dp"
android:orientation="vertical"
android:weightSum="1"
android:background="@drawable/contenedor_img_2_1_6_3" android:scaleType="centerCrop">



<LinearLayout
android:layout_width="match_parent"
android:layout_height="56dp"
android:orientation="vertical"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="false"
android:layout_alignParentStart="true"
android:background="@color/opacity"
android:gravity="center_vertical">

<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:textColor="#ffffff"
android:text="Akash Bangad"
android:textSize="14sp"
android:textStyle="bold"


/>

&lt;!&ndash; android:background="#7f000000"
android:singleLine="false"
android:paddingLeft="5dp"
android:paddingTop="5dp"
android:paddingRight="15dp"
android:paddingBottom="5dp"&ndash;&gt;

<TextView
android:id="@+id/email"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="inicio_texto_empresa"
android:textColor="#ffffffff"
android:layout_marginLeft="16dp"
/>
</LinearLayout>

<de.hdodenhof.circleimageview.CircleImageView
android:layout_width="70dp"
android:layout_height="70dp"
android:src="@drawable/barra_lateral_img_0_1"
android:layout_marginLeft="16dp"
android:layout_marginTop="38dp"
android:id="@+id/circleView"
android:adjustViewBounds="false" />
</RelativeLayout>-->

主要 Activity (如何调用)

mRecyclerView = (RecyclerView) findViewById(R.id.RecyclerView); // Assigning the RecyclerView Object to the xml View

mRecyclerView.setHasFixedSize(true); // Letting the system know that the list objects are of fixed size

mAdapter = new MyAdapter(TITLES, ICONS, NAME, EMAIL, PROFILE, this); // Creating the Adapter of MyAdapter class(which we are going to see in a bit)
// And passing the titles,icons,header view name, header view email,
// and header view profile picture

mRecyclerView.setAdapter(mAdapter);




mRecyclerView.addOnItemTouchListener(new RecyclerView.OnItemTouchListener() {
@Override
public boolean onInterceptTouchEvent(RecyclerView recyclerView, MotionEvent motionEvent) {
View child = recyclerView.findChildViewUnder(motionEvent.getX(), motionEvent.getY());


}

return false;
}

@Override
public void onTouchEvent(RecyclerView recyclerView, MotionEvent motionEvent) {

}
});

mLayoutManager = new LinearLayoutManager(this); // Creating a layout Manager

mRecyclerView.setLayoutManager(mLayoutManager); // Setting the layout Manager

最佳答案

but I don't know how I can add a footer

您可以为 HeaderView 做同样的事情。将页脚视为 RecyclerView 的一项:

您的 getItemCount 将返回

   @Override
public int getItemCount() {
return mNavTitles.length+2;
}

你将有一个方法调用 isPositionFooter

   private boolean isPositionHeader(int position) {
return position == getItemCount() - 1;
}

您将相应地更改 getItemViewType 以返回页脚类型:

 @Override
public int getItemViewType(int position) {
if (isPositionHeader(position)) {
return TYPE_HEADER;
} else if (isPositionFooter(position)) {
return TYPE_FOOTER;
}
return TYPE_ITEM;
}

关于android - Material 设计导航适配器页脚/页眉(如何添加页脚?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32452484/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com