In this posted I will talking about how to customize Header of ListView in android. I really tired to find more examples today I have solution to customize header of listview. let's start
Create new project in android studio.
and here is my code :
1. Create Custom Header layout
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#4333" android:orientation="vertical">
<TextView android:id="@+id/txtheader_excel_item" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginLeft="5dp" android:layout_marginStart="5dp" android:paddingTop="5dp" android:paddingBottom="5dp" android:text="Function for Math" android:textAllCaps="false" android:textColor="@android:color/black" android:textSize="18sp" android:textStyle="bold" tools:layout_editor_absoluteX="101dp" tools:layout_editor_absoluteY="2dp" />
</LinearLayout>
and next I create One Class call ExcelFragment extends from Fragment
import android.app.SearchManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SearchView;
import android.widget.Toast;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import java.util.ArrayList;
import java.util.List;
import android.widget.Filter;
public class ExcelFragment extends Fragment {
ListView listView;
TwoTextArrayAdapter adapter;
@Override public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
items = new ArrayList <>();
items.add(new Header("រូបមន្ត គណិតវិត្យា",getContext()));
items.add(new ListItem("០១- ", "រូបមន្ត កូស៊ីនីស",getActivity()));
items.add(new ListItem("០២-", "រូបមន្ត កូតង់សង់",getActivity()));
items.add(new ListItem("០៣-", "រូបមន្ត ស៊ីនីស",getActivity()));
items.add(new ListItem("០៤-", "រូបមន្ត ត្រីកោណមាត្រ",getActivity()));
items.add(new Header("រូបមន្ត ស្វីតនព្ធន្ត",getContext()));
items.add(new ListItem("Text 5", "Rabble rabble",getActivity()));
items.add(new ListItem("Text 6", "Rabble rabble",getActivity()));
items.add(new ListItem("Text 7", "Rabble rabble",getActivity()));
items.add(new ListItem("Text 8", "Rabble rabble",getActivity()));
items.add(new Header("Header 3",getContext()));
items.add(new ListItem("Text 9", "Rabble rabble",getActivity()));
items.add(new ListItem("Text 10", "Rabble rabble",getActivity()));
items.add(new ListItem("Text 11", "Rabble rabble",getActivity()));
items.add(new ListItem("Text 12", "Rabble rabble",getActivity()));
}
//Required Constructor public ExcelFragment(){}
@Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_excel,container,false);
// excelList = new ArrayList <HashMap<String, String>>(); listView =(ListView)v.findViewById(R.id.list_excel_lesson);
//Set Adapter for list adapter = new TwoTextArrayAdapter(getActivity(), items);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override public void onItemClick(AdapterView <?> adapterView, View view, int position, long l) {
Intent intent = new Intent(getContext(),SearchResultsActivity.class);
startActivity(intent);
}
});
return v;
}
}
Fragment Excel Layout fragment_excel.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:orientation="vertical" android:layout_height="match_parent">
<ListView android:id="@+id/list_excel_lesson" android:layout_width="match_parent" android:layout_height="match_parent" />
</LinearLayout>
Interface Item
import android.view.LayoutInflater;
import android.view.View;
public interface Item {
public int getViewType();
public View getView(LayoutInflater inflater, View convertView);
}
Class TwoTextArrayAdapter.java
import android.content.Context;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import java.util.List;
public class TwoTextArrayAdapter extends ArrayAdapter<Item> {
private static final int TYPE_ITEM = 0;
private static final int TYPE_SEPARATOR = 1;
private LayoutInflater mlaInflater;
public enum RowType{
LIST_ITEM,HEADER_ITEM }
public TwoTextArrayAdapter(@NonNull Context context, @NonNull List<Item> objects) {
super(context, 0, objects);
mlaInflater = LayoutInflater.from(context);
}
@Override public int getViewTypeCount() {
return RowType.values().length;
}
@Override public int getItemViewType(int position) {
return getItem(position).getViewType();
}
@Override public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
int rowType = getItemViewType(position);
View View;
if (convertView == null) {
holder = new ViewHolder();
switch (rowType) {
case TYPE_ITEM:
convertView = mlaInflater.inflate(R.layout.list_ex_item, null);
holder.View=getItem(position).getView(mlaInflater, convertView);
break;
case TYPE_SEPARATOR:
convertView = mlaInflater.inflate(R.layout.header_excel_item, null);
holder.View=getItem(position).getView(mlaInflater, convertView);
break;
}
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
return convertView;
//return getItem(position).getView(mlaInflater, convertView); }
public static class ViewHolder {
public View View; }
}
In MainActivity.java I create one method to add fragment
private void loadFragment(Fragment fragment) {
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.mycontianer, fragment);
transaction.addToBackStack(null);
transaction.commit();
}
In onCreate method I add loadFragment to load item by default
loadFragment(new ExcelFragment());
activity_main.xml
<FrameLayout android:id="@+id/mycontianer" android:layout_width="match_parent" android:layout_height="0dp"
app:layout_behavior="@string/appbar_scrolling_view_behavior" app:layout_constraintBottom_toTopOf="@+id/bottomNavigation" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/TopNavigation" tools:ignore="MissingConstraints">
</FrameLayout>
Let's run