{"id":18001587,"url":"https://github.com/hehonghui/common_adapter_viewholder","last_synced_at":"2025-03-26T08:30:51.176Z","repository":{"id":20214788,"uuid":"23486396","full_name":"hehonghui/common_adapter_viewholder","owner":"hehonghui","description":"common_adapter_viewholder","archived":false,"fork":false,"pushed_at":"2014-10-08T09:46:50.000Z","size":422,"stargazers_count":7,"open_issues_count":0,"forks_count":3,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-03-21T11:50:36.351Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/hehonghui.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-08-30T06:43:35.000Z","updated_at":"2019-10-17T13:59:24.000Z","dependencies_parsed_at":"2022-07-27T01:02:11.356Z","dependency_job_id":null,"html_url":"https://github.com/hehonghui/common_adapter_viewholder","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hehonghui%2Fcommon_adapter_viewholder","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hehonghui%2Fcommon_adapter_viewholder/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hehonghui%2Fcommon_adapter_viewholder/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hehonghui%2Fcommon_adapter_viewholder/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/hehonghui","download_url":"https://codeload.github.com/hehonghui/common_adapter_viewholder/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245618574,"owners_count":20645024,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-29T23:18:02.664Z","updated_at":"2025-03-26T08:30:50.646Z","avatar_url":"https://github.com/hehonghui.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"common_adapter_viewholder\n=========================\n\n\n# CommonAdapter的使用\n\n## 介绍与使用\n  一般我们在项目中使用到ListView和GridView组件都是都会用到Adapter，比较多的情况是继承自BaseAdapter，然后实现getCount、getView等方法，再使用ViewHolder来提高一下效率.我们看下面一个例子 :\n  \n### ListView布局文件\nfragment_main.xml :\n```xml\n\u003cRelativeLayout xmlns:android=\"http://schemas.android.com/apk/res/android\"\n    xmlns:tools=\"http://schemas.android.com/tools\"\n    android:layout_width=\"match_parent\"\n    android:layout_height=\"match_parent\"\n    android:paddingBottom=\"@dimen/activity_vertical_margin\"\n    android:paddingLeft=\"@dimen/activity_horizontal_margin\"\n    android:paddingRight=\"@dimen/activity_horizontal_margin\"\n    android:paddingTop=\"@dimen/activity_vertical_margin\"\n    tools:context=\"com.example.push_demo_1.MainActivity$PlaceholderFragment\" \u003e\n\n    \u003cListView\n        android:id=\"@+id/my_listview\"\n        android:layout_width=\"match_parent\"\n        android:layout_height=\"match_parent\" /\u003e\n\n\u003c/RelativeLayout\u003e\n```   \n\n### ListView子项的布局文件\nlistview_item_layout.xml :\n```xml\n\u003c?xml version=\"1.0\" encoding=\"utf-8\"?\u003e\n\u003cLinearLayout xmlns:android=\"http://schemas.android.com/apk/res/android\"\n    android:layout_width=\"match_parent\"\n    android:layout_height=\"match_parent\"\n    android:orientation=\"horizontal\" \u003e\n\n    \u003cTextView\n        android:id=\"@+id/my_textview\"\n        android:layout_width=\"wrap_content\"\n        android:layout_height=\"wrap_content\"\n        android:layout_marginLeft=\"20dp\"\n        android:textSize=\"18sp\" /\u003e\n\n\u003c/LinearLayout\u003e\n```    \n  \n### 我们通常情况下要写的Adapter代码\n```java\npublic class NormalAdapter extends BaseAdapter {\n\n    Context mContext;\n\n    LayoutInflater mInflater;\n\n    List\u003cString\u003e mDataList;\n\n    /**\n     * @param context\n     * @param data\n     */\n    public NormalAdapter(Context context, List\u003cString\u003e data) {\n        mContext = context;\n        mInflater = LayoutInflater.from(context);\n        mDataList = data;\n    }\n\n    @Override\n    public int getCount() {\n        return mDataList.size();\n    }\n\n    @Override\n    public Object getItem(int position) {\n        return mDataList.get(position);\n    }\n\n    @Override\n    public long getItemId(int position) {\n        return position;\n    }\n\n    @Override\n    public View getView(int position, View convertView, ViewGroup parent) {\n        ViewHolder viewHolder = null;\n        if (convertView == null) {\n            convertView = mInflater.inflate(R.layout.listview_item_layout, null, false);\n            viewHolder = new ViewHolder();\n            viewHolder.mTextView = (TextView) convertView.findViewById(R.id.my_textview);\n            convertView.setTag(viewHolder);\n        } else {\n            viewHolder = (ViewHolder) convertView.getTag();\n        }\n\n        viewHolder.mTextView.setText((String) getItem(position));\n        return convertView;\n    }\n\n    static class ViewHolder {\n        TextView mTextView;\n    }\n\n}\n```      \n\n   然而写过多遍以后我们发现我们总是重复地在写这些getCount、getItem、getView方法以及ViewHolder，导致了很多重复工作，于是我把这些重复工作抽象起来(以前也有在github上看到这样的通用Adapter实现)便于自己使用，也是自己学习的一个过程。下面我们看看使用CommonAdapter后我们做与上面同样的工作需要怎么写。\n   \n### 使用CommonAdapter后要写的代码\n```java\nCommonAdapter\u003cString\u003e listAdapter = new CommonAdapter\u003cString\u003e(getActivity(),\n                    R.layout.listview_item_layout, mockListViewItems()) {\n\n                @Override\n                protected void fillItemData(CommonViewHolder viewHolder, String item) {\n                    // 设置text\n                    viewHolder.setTextForTextView(R.id.my_textview, item);\n                }\n            }\n```    \n   可以看到，我们的代码量减少了很多，如果一个项目中有好几个ListView、GridView等组件，我们就不需要重复做那么多工作了。\n\n### 运行demo看效果吧 \n\t   \n\n\n## ViewFinder的使用\n\n  ViewFinder是一个在一个布局中找某个子控件的工具类,用户需要在使用时调用ViewFinder.initContentView函数来初始化ContentView，参数为Context和布局id。然后使用ViewFinder.findViewById来获取需要的view,findViewById为泛型方法,返回的view则直接是你接收的类型,而不需要进行强制类型转换.比如,以前我们在Activity中找一些子控件一般是这样 : \n```java\n@Override\nprotected void onCreate(Bundle savedInstanceState) {\n        super.onCreate(savedInstanceState);\n        setContentView(R.layout.activity_main);\n        //  查找子控件\n        TextView textView = (TextView)findViewById(R.id.my_textview); \n \t\tImageView imageView = (ImageView)findViewById(R.id.my_imageview); \n \t\tListView listView = (ListView)findViewById(R.id.my_listview);\n}\n```    \n   如果页面中的控件比较多，就会有很多的类型转换,而使用ViewFinder则免去了类型转换,示例如下 : \n```java\n@Override\nprotected void onCreate(Bundle savedInstanceState) {\n        super.onCreate(savedInstanceState);\n        setContentView(R.layout.activity_main);\n        // 初始化\n        ViewFinder.initContentView(this, R.layout.activity_main) ;\n        // 查找子控件\n        TextView textView = ViewFinder.findViewById(R.id.my_textview); \n \t\tImageView imageView = ViewFinder.findViewById(R.id.my_imageview); \n \t\tListView listView = ViewFinder.findViewById(R.id.my_listview);\n}\n```      \n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhehonghui%2Fcommon_adapter_viewholder","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhehonghui%2Fcommon_adapter_viewholder","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhehonghui%2Fcommon_adapter_viewholder/lists"}