2015-11-12 22 views

回答

1

的第一件事是如何顯示水平的ListView。我想推薦你使用RecyclerView來獲得更順暢的listView顯示。那麼如何在Horizo​​ntal中顯示recyclerView?這是我的教程一步一步。

首先,在XML文件應該是這樣的:

<android.support.v7.widget.RecyclerView 
     android:id="@+id/recyclerView" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_alignParentBottom="true" 
     android:scrollbars="vertical" /> 

其次,在活動的onCreate應該是這樣的:

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    recyclerViewItem = (RecyclerView) findViewById(R.id.recyclerView); 
    LinearLayoutManager itemslayoutManager = new LinearLayoutManager(getApplicationContext()); 
    itemslayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL); 
    recyclerViewItem.setLayoutManager(itemslayoutManager); 
    SampleAdapter sampleAdapter = new SampleAdapter(List<SampleData>); 
    recyclerViewItem.setAdapter(sampleAdapter); 
    return true; 
    } 

爲了解析JSON你應該使用庫調用:Retrofit代替AsyncTask 試試這個: Retrofit

0

activity_main.xml中

<android.support.v4.view.ViewPager 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:numColumns="auto_fit" 
    android:horizontalSpacing="4dp" 
    android:stretchMode="columnWidth" 
    android:id="@+id/gallery_child_list" 
    android:background="#E4E4E4" 
    android:verticalSpacing="4dp" /> 

Mainactivity

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    // Get the view from activity_main.xml 
    setContentView(R.layout.activity_main); 
    // Execute DownloadJSON AsyncTask 
    new DownloadJSON().execute(); 
} 

// DownloadJSON AsyncTask 
private class DownloadJSON extends AsyncTask<Void, Void, Void> { 

    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     // Create a progressdialog 
     mProgressDialog = new ProgressDialog(MainActivity.this); 
     // Set progressdialog message 
     mProgressDialog.setMessage("Loading..."); 
     mProgressDialog.setIndeterminate(false); 
     // Show progressdialog 
     mProgressDialog.show(); 
    } 

    @Override 
    protected Void doInBackground(Void... params) { 
     // Create an array 
     arraylist = new ArrayList<HashMap<String, String>>(); 
     // Retrieve JSON Objects from the given URL address 
     try { 
      HttpClient client = new DefaultHttpClient(); 
      HttpPost httppost = new HttpPost("YOUR_URL"); 
      HttpResponse response = client.execute(httppost); 
      HttpEntity resEntity = response.getEntity(); 
      String json = EntityUtils.toString(resEntity); 
      JSONObject jObject = new JSONObject(json); 
      // Locate the array name in JSON 
       HashMap<String, String> map = new HashMap<String, String>(); 
      JSONObject jsonobject = jObject.optJSONObject("online"); 
      for (int i = 0; i < jObject.length(); i++) { 
        map.put("date", jsonobject.getString("men")); 
        map.put("flag", jsonobject.getString("kids")); 
        map.put("flag", jsonobject.getString("women")); 
        // Set the JSON Objects into the array 
        arraylist.add(map); 
       } 
       } catch (Exception e1) { 
       e1.printStackTrace(); 
      } 
     return null; 
    } 
     @Override 
    protected void onPostExecute(Void args) { 
     viewPager = (ViewPager) findViewById(R.id.pager); 
     adapter = new ViewPagerAdapter(MainActivity.this, arraylist); 
     viewPager.setAdapter(adapter); 
     // Close the progressdialog 
     mProgressDialog.dismiss(); 
     adapter.notifyDataSetChanged(); 

    } 
} 

viewpageradapter

public class ViewPagerAdapter extends PagerAdapter { 
// Declare Variables 
Context context; 
LayoutInflater inflater; 
ArrayList<HashMap<String, String>> data; 
ImageLoader imageLoader; 
HashMap<String, String> resultp = new HashMap<String, String>(); 

public ViewPagerAdapter(Context context, ArrayList<HashMap<String, String>> arraylist) { 
    this.context = context; 
    data = arraylist; 
    imageLoader = new ImageLoader(context); 
} 

@Override 
public boolean isViewFromObject(View view, Object object) { 
    return view == ((RelativeLayout) object); 
} 

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

     @Override 
public Object instantiateItem(ViewGroup container, int position) { 

    // Declare Variables 
    ImageView imgflag; 

    inflater = (LayoutInflater) context 
      .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    View itemView = inflater.inflate(R.layout.viewpager_item, container,false); 

    // Locate the ImageView in viewpager_item.xml 
    imgflag = (ImageView) itemView.findViewById(R.id.flag); 
      // Capture position 
      resultp = data.get(position); 
      // Capture position and set results to the ImageView 
      // Passes flag images URL into ImageLoader.class 
      imageLoader.DisplayImage(resultp.get(MainActivity.FLAG), imgflag); 
      // Add viewpager_item.xml to ViewPager 
      ((ViewPager) container).addView(itemView); 

    return itemView; 
} 

@Override 
public void destroyItem(ViewGroup container, int position, Object object) { 
    // Remove viewpager_item.xml from ViewPager 
    ((ViewPager) container).removeView((RelativeLayout) object); 

} 
} 
+0

獲取JSON數據基於您的JSON網址。 如果你沒有JSON數組,這個JSON將工作。 –