如何在列表视图中添加颜色,背景或动画

时间:2016-05-12 09:00:14

标签: android listview fragment

您好我的android片段中有一个简单的列表视图如何添加颜色,背景或动画 感谢

编辑 我想添加2个交替的背景图像exp: row1 image1,row2 image2,row3 image1,row4:image2 ........ 第一个错误是:无法解析符号arrayList

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootview = inflater.inflate(R.layout.fragment_users, container, false);
        listView  = (ListView)   rootview.findViewById(R.id.listView);
        listView.setOnItemClickListener(this);
        getJSON();
        return rootview;
    }

    private void showUser(){
        JSONObject jsonObject = null;
        ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String, String>>();
        try {
            jsonObject = new JSONObject(JSON_STRING);
            JSONArray result = jsonObject.getJSONArray(TAG_JSON_ARRAY);
            for(int i = 0; i<result.length(); i++){
                JSONObject jo = result.getJSONObject(i);
                String name = jo.getString("Nom");
                String email = jo.getString("Email");
                String login = jo.getString("Login");
                HashMap<String,String> employees = new HashMap<>();
                employees.put("name",name);
                employees.put("email",email);
                employees.put("login",login);
                list.add(employees);
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }

        ListAdapter adapter = new SimpleAdapter(
                getActivity(), list, R.layout.list_row,
                new String[]{"name","email"},
                new int[]{R.id.nom, R.id.email2});
        AboutAdapter aboutAdapter = new AboutAdapter(UsersFragment.this, arrayList);
        listView.setAdapter(aboutAdapter);
        listView.setAdapter(adapter);
    }
    private void getJSON() {
        class GetJSON extends AsyncTask<Void, Void, String> {
            ProgressDialog loading;
            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                loading = ProgressDialog.show(getActivity(), "Fetching Data", "Wait...", false, false);
            }
            @Override
            protected void onPostExecute(String s) {
                super.onPostExecute(s);
                loading.dismiss();
                JSON_STRING = s;
                showUser();
            }
            @Override
            protected String doInBackground(Void... params) {
                RequestHandler rh = new RequestHandler();
                String s = rh.sendGetRequest(URL);
                return s;
            }
        }
        GetJSON gj = new GetJSON();
        gj.execute();
    }

fragment_user.xml

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

    <ListView android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/list_selector"
        android:drawSelectorOnTop="false"/>
</FrameLayout>

list_row.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:id="@+id/nom"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/email2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

AboutAdapter.java 第二个错误错误:(6,23)不允许字符串类型(在'drawable'处,值为'image1')。 我如何使用secon image2

public class AboutAdapter extends BaseAdapter {
    private LayoutInflater inflater;
    private Context mcontext;
    private String[] arrayList;

    public AboutAdapter(Context context, String[] arrayList) {
        this.mcontext = context;
        this.arrayList= arrayList;
        inflater = LayoutInflater.from(mcontext);
    }

    @Override
    public int getCount() {
        return arrayList.length;
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }
    public class Holder {
        ImageView imag;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View vi = convertView;
        Holder holder;
        if (vi == null) {
            vi = inflater.inflate(R.layout.list_row, null);
            holder = new Holder();
            holder.imag = (ImageView) vi
                    .findViewById(R.id.image1);
            vi.setTag(holder);
        } else {
            holder = (Holder) vi.getTag();
        }
// same as for image pass array of image and set
        holder.imag.setBackgroundResource(R.drawable.list_selector);
        return vi;
    }
}

list_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:state_selected="false"
        android:state_pressed="false"
    android:drawable="image1"/>
    <item android:state_pressed="true"
        android:drawable="@color/itemselected" />
    <item android:state_selected="true"
        android:state_pressed="false"
        android:drawable="@color/itemselected" />
</selector> 

2 个答案:

答案 0 :(得分:0)

您可以将ListView的背景设置为颜色:

listView.setBackgroundColor(Color.WHITE);

或将其设置为drawable:

int resource = getResources().getIdentifier("subtle_white_gradient", "drawable", "com.my.package_name");
ListView.setBackgroundResource(resource);

或者你也可以在布局中定义

<ListView  
        android:id="@+id/list" 
        android:layout_width="fill_parent"  
        android:layout_height="fill_parent"
        android:background="@color/blue"/>

您可以通过以下代码设置随机背景颜色...... 随机颜色

Random rnd = new Random(); 
int color = Color.argb(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));   
listView.setBackgroundColor(color);

并传递图像,制作自定义适配器并将图像数组传入其中。

 public class AboutAdapter extends BaseAdapter {
        private LayoutInflater inflater;
        private Context mcontext;
        private Strig[] arrayList;

        public AboutAdapter(Context context, Strig[] arrayList) {
            this.mcontext = context;
            this.arrayList= arrayList;
            inflater = LayoutInflater.from(mcontext);
        }

        @Override
        public int getCount() {
            return arrayList.length;
        }

        @Override
        public Object getItem(int position) {
            return position;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        public class Holder {            
            ImageView imag;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View vi = convertView;
            Holder holder;
            if (vi == null) {
                vi = inflater.inflate(R.layout.raw_aboutlist, null);
                holder = new Holder();

                holder.imag = (ImageView) vi
                        .findViewById(R.id.raw_about_img);

                vi.setTag(holder);

            } else {
                holder = (Holder) vi.getTag();
            }



// same as for image pass array of image and set 

    holder.imag.setBackgroundResource(R.drawable.explr_img_selector);
            return vi;
        }

    }

然后在listview中设置适配器,如下所示

AboutAdapter aboutAdapter = new AboutAdapter(AboutActivity.this, arrayList);
listView.setAdapter(aboutAdapter);

答案 1 :(得分:0)

按xml,

<ListView  
    android:id="@+id/list" 
    android:layout_width="fill_parent"  
    android:layout_height="fill_parent"
    android:background="#353533"/>

<ListView  
    android:id="@+id/list" 
    android:layout_width="fill_parent"  
    android:layout_height="fill_parent"
    android:background="@drawable/list_selector"/>
  

list_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="true" >
 <shape android:shape="rectangle"  >
     <corners android:radius="3dip" />
     <stroke android:width="1dip" android:color="#5e7974" />
     <gradient android:angle="-90"        
       android:startColor="@android:color/holo_blue_dark" 
       android:endColor="@android:color/holo_blue_bright"  />            
 </shape>
</item>

<item android:state_focused="true">
 <shape android:shape="rectangle"  >
     <corners android:radius="3dip" />
     <stroke android:width="1dip" android:color="#5e7974" />
     <solid android:color="@android:color/holo_blue_dark"/>       
 </shape>
 </item>  

<item >
<shape android:shape="rectangle"  >
     <corners android:radius="3dip" />
     <stroke android:width="1dip"      
          android:color="@android:color/holo_blue_dark" />
     <gradient android:angle="-90" 
         android:startColor="@android:color/holo_blue_dark" 
         android:endColor="@android:color/holo_blue_dark" />            
 </shape>
 </item>
 </selector>

或java,

listView.setBackgroundColor(Color.GRAY);

这可能对你有所帮助。