2016-03-07 45 views
0

我想显示在水平滚动视图中的每个JSON对象分别手段ID在分开的水平滚动图,同为resType,姓名和URL.So我将有总共4个水平滚动的意见如何diplay JSON对象在滚动视图

 JsonObjectRequest jor = new JsonObjectRequest(url, null, 
      new Response.Listener<JSONObject>() { 
       @Override 
       public void onResponse(JSONObject response) { 

        try {       

         JSONArray ja = response.getJSONArray("results"); 

         ArrayList<Details> myModelList = new ArrayList<Details>(); 
         Details mymodel = null; 
         for (int i = 0; i < ja.length(); i++) { 

          JSONObject jsonObject = ja.getJSONObject(i); 
          mymodel = new Details(); 
          mymodel.id = Integer.parseInt(jsonObject.optString("id").toString()); 
          mymodel.url = jsonObject.getString("resLink"); 
          mymodel.resType = jsonObject.getString("resType"); 
          mymodel.name = jsonObject.getString("resName"); 

          myModelList.add(mymodel); 
           // setData(); 
          // 

         } 


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

回答

0

您可以使用Horizo​​ntalScrollView这一点。这里有一些代码让你开始。首先,您可以使用Horizo​​ntalScrollView创建一个布局,在其中您将放置该字符串(或您要放置的值)的位置内的一个TextView

hor_scrl_view_item

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

    <TextView 
     android:id="@+id/tv" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:paddingRight="5dp" 
     /> 

</HorizontalScrollView> 

下一个是你把一个视图,将作为Horizo​​ntalScrollView的列表的容器。

activity_main

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    android:weightSum="5" 
    tools:context=".MainActivity"> 

    <LinearLayout 
     android:id="@+id/container" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical" /> 

</LinearLayout> 

然后加入Horizo​​ntalScrollViews动态容器视图。

MainActivity

public class MainActivity extends AppCompatActivity { 

    private LinearLayout llMain; 

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

     initViews(); 
    } 


    private void initViews() { 
     llMain = (LinearLayout) findViewById(R.id.container); 

     List<String> ids = Arrays.asList("1732150721937507n15498017b329587b213985v7b2159832175bv980127b59817b295v87b219857b2198075v9017", 
       "21938579817v98219857b13215v982175bv982175v9018275v980217bv52", 
       "2935871n2980v7b25987b98712375bv9832175bv9821735b9802137b5v90215v9821375bv9802175bv982175v9802175v98175vb980217b5v98017bv3"); 

     for (int i = 0; i < ids.size(); i++) { 
      llMain.addView(addNewItem(ids.get(i))); 
     } 

    } 

    private HorizontalScrollView addNewItem(String str) { 
     HorizontalScrollView sv = (HorizontalScrollView) getLayoutInflater().inflate(R.layout.json_details_scrollview, null); 
     TextView tv = (TextView) sv.findViewById(R.id.tv); 
     tv.setText(str); 

     return sv; 
    } 

} 

下面是截图我跑后。

enter image description here

希望这有助于。 :)

相关问题