2016-05-12 41 views
0

你好,我在我的Android片段的簡單列表視圖我怎麼能我如何添加顏色,背景或動畫到ListView

編輯 我要添加2交替的背景圖像添加顏色,背景或動畫 感謝EXP: ROW1圖像1,2行圖像2,ROW3圖像1,ROW4:圖像2 ........ 第一個錯誤是:不能解析符號的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)字符串類型不允許(在 '可拉伸',值爲 'image1的')。 我如何使用塞康圖像2

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> 
+0

請按照[如何創建最小,完整和可驗證示例](http://stackoverflow.com/help/mcve)包含相關代碼 – MikeT

+0

那麼,您的問題是否已解決? –

+0

還沒有。感謝您的跟進,我用我的代碼 –

回答

0

您可以將您的ListView的背景設定爲一個顏色:

listView.setBackgroundColor(Color.WHITE); 

或將其設置爲可繪製:

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; 
     } 

    } 

,然後設置在列表視圖適配器如下所示

AboutAdapter aboutAdapter = new AboutAdapter(AboutActivity.this, arrayList); 
listView.setAdapter(aboutAdapter); 
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); 

這可能幫助你。

+0

更新了我的帖子,謝謝它的幫助。現在我有兩個圖像我想在背景中相互設置它們,例如row1:blue,row2:white,row3:blue,row4:white但是由於我從數據庫獲取數據而不知道行數。這是可以改變的 –

+0

是的,如果你傳遞'n'項目到列表視圖的手段,背景將出現在所有項目 –

+0

謝謝,但我不明白我可以聲明image1和image2,因爲我沒有相同的背景所有項目 –