如何在Android Studio中使用库项目?

时间:2014-12-16 01:28:05

标签: android listview module android-studio

我正在使用这个模块或其他模块来使我的列表视图中的节标题保持在顶部。

http://applidium.github.io/HeaderListView/

现在在readMe文件中说

1. Import the project in Eclipse then add it to the build path of your project.
2. Replace your ListView with HeaderListView
3. Implement a subclass of SectionAdapter
4. Set it to your HeaderListView with setAdapter(SectionAdapter adapter)

现在我将整个项目文件导入Android Studio以使其正常工作吗?我需要在gradle文件中添加任何内容吗?

我会做File>导入项目或导入模块?

或者如何从github

添加和使用这个项目

https://github.com/vinc3m1/android-segmentedradiobutton

感谢您的帮助:)

修改

我现在需要帮助使用我的列表视图和数据设置HeaderView他是我目前如何使用不粘的标题的ListView。我希望它设置好,所以他们坚持。

这是我的数据,其中键是节标题,值是单元格。

linkedHashMap = {Pizza=[Cheese Pizza - Slice, Pepperoni Pizza, Tomato Bruschetta Flatbread Pizza, Herb 
               Seasoned Breadsticks, Spaghetti Sauce with Tomato Bits], 
       Salad=[Salad Bar Station, Green Pepper & Tomato Salad, Lo Mein Noodle Salad],
       Cold Cereal=[Miscellaneous/Peripherals]
       Dessert=[Carnival Cookies, Sweet Cinnamon Brownies, Oreo Crumble Pudding Cup, 
                 Cherry Jell-O Parfait]}

这是我的设置。

MyCustomAdapter mAdapter = new MyCustomAdapter();
    for (int i = 0; i < linkedHashMap.size(); i++) {
        Object[] headerArray = linkedHashMap.keySet().toArray();
        String headerString = headerArray[i].toString();
        mAdapter.addSeparatorItem(headerString);
        for (int i2 = 0; i2 < linkedHashMap.get(headerString).size(); i2++) {
            mAdapter.addItem(linkedHashMap.get(headerString).get(i2));
        }
    }

而且这是我的适配器

public class MyCustomAdapter extends BaseAdapter{
    ArrayList mData = new ArrayList();
    LayoutInflater mLayoutInflater;

    private static final int TYPE_ITEM = 0;
    private static final int TYPE_SEPARATOR = 1;
    private static final int TYPE_MAX_COUNT = TYPE_SEPARATOR + 1;

    private TreeSet mSeparatorsSet = new TreeSet();

    public MyCustomAdapter() {
        mLayoutInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public void addItem(final String item) {
        mData.add(item);
        notifyDataSetChanged();
    }

    public void addSeparatorItem(final String item) {
        mData.add(item);
        // save separator position
        mSeparatorsSet.add(mData.size() - 1);
        notifyDataSetChanged();
    }

    @Override
    public int getItemViewType(int position) {
        return mSeparatorsSet.contains(position) ? TYPE_SEPARATOR : TYPE_ITEM;
    }

    @Override
    public int getViewTypeCount() {
        return TYPE_MAX_COUNT;
    }

    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public String getItem(int position) {
        return mData.get(position).toString();
    }

    @Override
    public long getItemId(int position) {
        return position;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder holder = null;
        int type = getItemViewType(position);
        System.out.println("getView " + position + " " + convertView + " type = " + type);
        if (convertView == null) {
            holder = new ViewHolder();
            switch (type) {
                case TYPE_ITEM:
                    convertView = mLayoutInflater.inflate(R.layout.item_cell_view, parent, false);
                    holder.textView = (TextView)convertView.findViewById(R.id.text);
                    break;
                case TYPE_SEPARATOR:
                    Context context = DiningItemsActivity.this;
                    convertView = mLayoutInflater.inflate(R.layout.header_cell_view, parent, false);
                    holder.textView = (TextView)convertView.findViewById(R.id.textSeparator);
                    convertView.setBackgroundColor(colorBar);
                    break;
            }
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder)convertView.getTag();
        }
        holder.textView.setText(mData.get(position).toString());
        return convertView;
    }

    public class ViewHolder {
        public TextView textView;
    }


}

2 个答案:

答案 0 :(得分:2)

该库有一个演示,我建议你看一下,它是你在代码中所做的一切,但是以一种很好的方式,例如库分离了获取标题视图和行视图或行视图数或标题视图。我认为方法名称是不言自明的,所以试试看:

public class MainActivity extends Activity {

    HeaderListView mHeaderListView;
    LinkedHashMap<String, String[]> mLinkedHashMap;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);  

        mHeaderListView = (HeaderListView)findViewById(R.id.HeaderListView_MainActivity);
        mLinkedHashMap = new LinkedHashMap<>();
        mLinkedHashMap.put("Pizza", new String[]{"Cheese Pizza - Slice","Pepperoni Pizza","Tomato Bruschetta Flatbread Pizza",
                "Herb Seasoned Breadsticks","Spaghetti Sauce with Tomato Bits"});

        mLinkedHashMap.put("Salad", new String[]{"Salad Bar","Green Pepper & Tomato Salad","Lo Mein Noodle Salad"});

        mLinkedHashMap.put("Cold Cereal", new String[]{"Miscellaneous/Peripherals"});

        mLinkedHashMap.put("Dessert", new String[]{"Carnival Cookies","Sweet Cinnamon Brownies","Oreo Crumble Pudding Cup", 
        "Cherry Jell-O Parfait"});


        mHeaderListView.setAdapter(new SectionAdapter() {


            @Override
            public int numberOfSections() {

                return mLinkedHashMap.keySet().toArray().length;
            }

            @Override
            public int numberOfRows(int section) {

                if(section >=0){
                    String sectionKey = (String)mLinkedHashMap.keySet().toArray()[section];
                    int numOfRows = mLinkedHashMap.get(sectionKey).length;
                    return numOfRows;
                }else{
                    return 0;
                }
            }

            @Override
            public boolean hasSectionHeaderView(int section) {
                return true;
            }

            @Override
            public View getRowView(int section, int row, View convertView,
                    ViewGroup parent) {

                ViewHolder holder = null;
                if (convertView == null) {
                    holder = new ViewHolder();
                    LayoutInflater mLayoutInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
                    convertView = mLayoutInflater.inflate(R.layout.item_cell_view, parent, false);
                    holder.textView = (TextView)convertView.findViewById(R.id.text);
                    convertView.setTag(holder);
                } else {
                    holder = (ViewHolder)convertView.getTag();
                }
                String sectionKey = (String)mLinkedHashMap.keySet().toArray()[section];
                holder.textView.setText(mLinkedHashMap.get(sectionKey)[row]);
                return convertView;

            }

            @Override
            public Object getRowItem(int section, int row) {

                return ((String[])mLinkedHashMap.keySet().toArray()[section])[row];
            }

            @Override
            public View getSectionHeaderView(int section, View convertView,
                    ViewGroup parent) {
                ViewHolder holder = null;
                LayoutInflater mLayoutInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
                if (convertView == null) {
                    holder = new ViewHolder();          
                    convertView = mLayoutInflater.inflate(R.layout.header_cell_view, parent, false);
                    holder.textView = (TextView)convertView.findViewById(R.id.textSeparator);
                    convertView.setBackgroundColor(Color.parseColor("#FF0000"));


                    convertView.setTag(holder);
                } else {
                    holder = (ViewHolder)convertView.getTag();
                }
                holder.textView.setText((String)(mLinkedHashMap.keySet().toArray()[section]));

                return convertView;
            }
        });


    }

    public static class ViewHolder {
        public TextView textView;
    }

}

item_cell_view.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:orientation="vertical" >

    <TextView 
        android:id="@+id/text"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:textSize="12sp"  
        />


</LinearLayout>

header_cell_view.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:orientation="vertical" >

    <TextView 
        android:id="@+id/textSeparator"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:textSize="12sp"  
        />


</LinearLayout>

activity_main.xml中:

<?xml version="1.0" encoding="utf-8"?>
 <com.example.your package name of HeaderListView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/HeaderListView_MainActivity"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#eff4ee"
        android:divider="#000000"
        android:dividerHeight="2dp"
        android:padding="8dp" />

答案 1 :(得分:0)

对于HeaderListView,您可以获取代码here,将HeaderListView文件夹导入为模块(导入模块)

第二,只需克隆并导入项目。这是一个示例代码。