转自:http://blog.csdn.net/anddroid_lanyan/article/details/41895631
网上开源项目:https://github.com/beworker/pinned-section-listview,该项目用的是ArrayAdapter()..不太适合我,稍作修改,仿的微信界面
PinnedSectionListView修改了一行代码,不然会报错,代码如下
void createPinnedShadow(int position) {}》》
[java] view
plaincopy
- if (layoutParams == null) {
- //layoutParams = (LayoutParams) generateDefaultLayoutParams();
- layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
- pinnedView.setLayoutParams(layoutParams);
- }
效果图:
具体代码AdapterListView:
[java] view
plaincopy
- package pinneactivity.example.ui;
- import java.util.ArrayList;
- import pinneactivity.example.ui.PinnedSectionListView.PinnedSectionListAdapter;
- import android.content.Context;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.BaseAdapter;
- import android.widget.ImageView;
- import android.widget.TextView;
- /*
- *
- * This is company of adapter
- *
- * @author YangWenlong
- */
- public class AdapterListView extends BaseAdapter implements PinnedSectionListAdapter{
- private ArrayList<Bean> list;
- private Context context;
- public ArrayList<Bean> getList() {
- return list;
- }
- public void setList(ArrayList<Bean> list) {
- if(list!=null){
- this.list = list;
- }else{
- list=new ArrayList<Bean>();
- }
- }
- public AdapterListView(Context context,ArrayList<Bean> list){
- this.setList(list);
- this.context=context;
- }
- @Override
- public int getCount() {
- // TODO Auto-generated method stub
- return list.size();
- }
- @Override
- public Bean getItem(int position) {
- // TODO Auto-generated method stub
- return list.get(position);
- }
- @Override
- public long getItemId(int position) {
- // TODO Auto-generated method stub
- return position;
- }
- @Override
- public View getView(int position, View converView, ViewGroup viewGrop) {
- // TODO Auto-generated method stub
- ViewHolder vh=null;
- if(converView==null){
- vh=new ViewHolder();
- converView=LayoutInflater.from(context).inflate(R.layout.item, null);
- vh.company_item=(TextView)converView.findViewById(R.id.title);
- vh.image=(ImageView)converView.findViewById(R.id.imageView1);
- converView.setTag(vh);
- }else{
- vh=(ViewHolder) converView.getTag();
- }
- Bean bean=getItem(position);
- vh.company_item.setText(bean.text);
- if (bean.type == Bean.SECTION) {
- vh.company_item.setBackgroundResource(R.drawable.bao_headview_bg);
- vh.image.setVisibility(View.GONE);
- }else{
- vh.company_item.setBackgroundResource(R.drawable.details_layout_bg);
- vh.image.setVisibility(View.VISIBLE);
- }
- return converView;
- }
- @Override
- public boolean isItemViewTypePinned(int viewType) {
- // TODO Auto-generated method stub
- return viewType == Bean.SECTION;//0是标题,1是内容
- }
- @Override
- public int getViewTypeCount() {
- return 2;//2种view的类型 baseAdapter中得方法
- }
- @Override
- public int getItemViewType(int position) {
- return ((Bean)getItem(position)).type;
- }
- public void refresh(ArrayList<Bean> arr){
- setList(arr);
- notifyDataSetChanged();
- }
- }
HeadView:
[java] view
plaincopy
- package pinneactivity.example.ui;
- import android.app.Activity;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.LinearLayout;
- import android.widget.Toast;
- /*
- * Fail view
- *
- * @author YangWenlong
- */
- public class HeadView implements OnClickListener{
- private View view = null;
- private Activity activity;
- private LinearLayout newFriends;
- private LinearLayout grop;
- private LinearLayout tag;
- private LinearLayout publicNumber;
- private static HeadView headView = null;
- public static HeadView getInstance(Activity activity) {
- headView = new HeadView(activity);
- return headView;
- }
- public HeadView(Activity activity) {
- view=activity.getLayoutInflater().inflate(R.layout.item_head, null);
- this.activity=activity;
- }
- public HeadView initialization(){
- newFriends=(LinearLayout)view.findViewById(R.id.layout_new);
- grop=(LinearLayout)view.findViewById(R.id.layout_group);
- tag=(LinearLayout)view.findViewById(R.id.layout_tag);
- publicNumber=(LinearLayout)view.findViewById(R.id.layout_public);
- newFriends.setOnClickListener(this);
- grop.setOnClickListener(this);
- tag.setOnClickListener(this);
- publicNumber.setOnClickListener(this);
- return headView;
- }
- public View getView(){
- return view;
- }
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- if(v==newFriends){
- Toast.makeText(activity, "新朋友", Toast.LENGTH_SHORT).show();
- }else if(v==grop){
- Toast.makeText(activity, "群聊", Toast.LENGTH_SHORT).show();
- }else if(v==tag){
- Toast.makeText(activity, "标签", Toast.LENGTH_SHORT).show();
- }else if(v==publicNumber){
- Toast.makeText(activity, "公众号", Toast.LENGTH_SHORT).show();
- }
- }
- }
项目下载:http://download.csdn.net/detail/anddroid_lanyan/8252891
时间: 2024-12-02 07:41:26