安卓学习笔记_Day_04

目录

可折叠的悬浮按钮(FloatingActionButton)

ListView的简单使用:

SimpleAdapter的简单使用:

自动完成文本框(AutoCompleteTextView)


可折叠的悬浮按钮(FloatingActionButton)

使用前要在gradle里添加依赖库:

implementation 'com.android.support:design:28.0.0'
android:clickable="true"
app:rippleColor="#38AEE4"
android:backgroundTint="#E7A94E"
app:fabSize="normal"
android:src="@drawable/plus"

app:rippleColor:指定按钮被单击时的波纹颜色

app:fabSize:按钮的尺寸

android:backgroundTint:设置按钮的填充色

ListView的简单使用:

    

    

编写自己的item文件:





在主活动中创建适配器,并使用自己编写的item文件作为 子项目:


public class MainActivity extends AppCompatActivity {

    ListView listView1;
    String [] a = new String[] {
            "11111","22222","33433"
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        listView1 = findViewById(R.id.list1);
         ArrayAdapter arrayAdapter = new ArrayAdapter(this,R.layout.item1,a);

         listView1.setAdapter(arrayAdapter);
        
    }
}

SimpleAdapter的简单使用:

SimpleAdapter(Context context, List> data,
        @LayoutRes int resource, String[] from, @IdRes int[] to) 

SimpleAdapter需要五个参数:

第一个:Context对象

第二个:List>类型的集合对象,该集合中每一个Map 对象生成一个列表项。

第三个:界面布局的id

第四个:一个String[]类型的参数,决定提取Map对象中哪些key对应的value来生成列表项。

第五个:一个String[]类型的参数,决定填充哪些组件。

第一步,创建数据:

String [] names = new String[]{"年少" ,"的我喜欢" ,"一个人" , "在海边"};
String [] descs = new String[]{"年少的我喜欢一个人在海边" , "卷起裤管光着脚丫踩在沙滩上" , "总是幻想海洋的尽头有另一个世界" , "总是以为勇敢的水手是真正的男儿"};
int [] imagesIds = new int[]{R.drawable.tou11,R.drawable.tou22,R.drawable.tu1,R.drawable.tu2};

第二步,创建一个List集合,集合里的元素是Map:

//创建一个list集合,里面的元素是map
        List> listItems = new ArrayList<>();
        for (int i = 0;i listItem = new HashMap<>();
            listItem.put("header",imagesIds[i]);
            listItem.put("personName",names[i]);
            listItem.put("desc",descs[i]);
            listItems.add(listItem);
        }

第三步,创建适配器:

//创建一个SimpleAdapter
        SimpleAdapter adapter = new SimpleAdapter(this,listItems,R.layout.item2,new String[]{"personName","header","desc"}
        ,new int[]{R.id.name,R.id.header,R.id.desc});

        listView1 = findViewById(R.id.list1);

 

自己创建的列表项item2.xml:




    
    

        
         
    

自动完成文本框(AutoCompleteTextView,MultiAutoCompleteTextView)

 

android:completionHint="请输入"             //下拉栏提示
android:completionThreshold="1"            //设置至少输入几个字符才会显示提示

设置适配器,以及下拉栏背景:

 ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_dropdown_item_1line,names);
        auto.setDropDownBackgroundResource(R.drawable.cat1);
        auto.setAdapter(adapter);

 


public class MainActivity extends Activity
{
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // 创建一个BaseExpandableListAdapter对象
        BaseExpandableListAdapter adapter = new BaseExpandableListAdapter()
        {
            int[] logos = new int[]{R.drawable.plus, R.drawable.plus, R.drawable.plus};
            String[] armTypes = new String[]{"神族兵种", "虫族兵种", "人族兵种"};
            String[][] arms = new String[][]{
                    new String[]{"狂战士", "龙骑士", "黑暗圣堂", "电兵"},
                    new String[]{"小狗", "刺蛇", "飞龙", "自爆飞机"},
                    new String[]{"机枪兵", "护士MM", "幽灵"}};

            @Override
            public int getGroupCount()
            {
                return armTypes.length;
            }

            @Override
            public int getChildrenCount(int groupPosition)
            {
                return arms[groupPosition].length;
            }

            // 获取指定组位置处的组数据
            @Override
            public Object getGroup(int groupPosition)
            {
                return armTypes[groupPosition];
            }

            // 获取指定组位置、指定子列表项处的子列表项数据
            @Override
            public Object getChild(int groupPosition, int childPosition)
            {
                return arms[groupPosition][childPosition];
            }

            @Override
            public long getGroupId(int groupPosition)
            {
                return groupPosition;
            }

            @Override
            public long getChildId(int groupPosition, int childPosition)
            {
                return childPosition;
            }

            @Override
            public boolean hasStableIds()
            {
                return true;
            }

            // 该方法决定每个组选项的外观
            @Override
            public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent)
            {
                LinearLayout ll;
                ViewHolder viewHolder;
                if (convertView == null) {
                    ll = new LinearLayout(MainActivity.this);
                    ll.setOrientation(LinearLayout.HORIZONTAL);
                    ImageView logo = new ImageView(MainActivity.this);
                    ll.addView(logo);
                    TextView textView = this.getTextView();
                    ll.addView(textView);
                    viewHolder = new ViewHolder(logo, textView);
                    ll.setTag(viewHolder);
                } else {
                    ll = (LinearLayout) convertView;
                    viewHolder = (ViewHolder) ll.getTag();
                }
                viewHolder.imageView.setImageResource(logos[groupPosition]);
                viewHolder.textView.setText(getGroup(groupPosition).toString());
                return ll;
            }

            // 该方法决定每个子选项的外观
            @Override
            public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent)
            {
                TextView textView;
                if (convertView == null) {
                    textView = this.getTextView();
                } else {
                    textView = (TextView) convertView;
                }
                textView.setText(getChild(groupPosition, childPosition).toString());
                return textView;
            }
            @Override
            public boolean isChildSelectable(int groupPosition, int childPosition)
            {
                return true;
            }

            private TextView getTextView()
            {
                TextView textView = new TextView(MainActivity.this);
                AbsListView.LayoutParams lp = new AbsListView.LayoutParams(ViewGroup.LayoutParams
                        .MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                textView.setLayoutParams(lp);
                textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.START);
                textView.setPadding(36, 10, 0, 10);
                textView.setTextSize(20f);
                return textView;
            }
        };
        ExpandableListView expandListView = findViewById(R.id.list);
        expandListView.setAdapter(adapter);
    }
    class ViewHolder{
        ImageView imageView;
        TextView textView;
        public ViewHolder(ImageView imageView, TextView textView)
        {
            this.imageView = imageView;
            this.textView = textView;
        }
    }
}

 

你可能感兴趣的:(自学笔记)