在这里完成菜鸟所以请耐心等待。我有一个活动从我的服务器加载数据并在Listview中显示它。现在我需要能够基于TabHost重新排序该列表。我使用的是MaterialTabHost(https://github.com/neokree/MaterialTabs)
这是我用来填充列表视图的代码:
listView = (ListView) findViewById(R.id.list);
adapter = new CustomListAdapter(this, movieList);
listView.setAdapter(adapter);
pDialog = new ProgressDialog(this);
// Showing progress dialog before making http request
pDialog.setMessage("Loading...");
pDialog.show();
// Creating volley request obj
final Double finalLat = lat;
final Double finalLon = lon;
final Double finalLat1 = lat;
final Double finalLon1 = lon;
JsonArrayRequest movieReq = new JsonArrayRequest(url,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(final JSONArray response) {
Log.d(TAG, response.toString());
hidePDialog();
// Parsing json
for (int i = 0; i < response.length(); i++) {
try {
JSONObject obj = response.getJSONObject(i);
viewer movie = new viewer();
LatLng point1 = new LatLng(obj.getDouble("lat"), obj.getDouble("long"));
LatLng point2 = new LatLng(finalLat, finalLon);
distanceInMiles = LatLngTool.distance(point1, point2, LengthUnit.MILE);
movie.setTitle(obj.getString("first_name"));
movie.setThumbnailUrl(obj.getString("pic"));
//movie.setRating(((String) obj.getString("experience")));
movie.setYear(Double.parseDouble(String.format("%.2f", distanceInMiles)));
} catch (JSONException e) {
e.printStackTrace();
}
}
// notifying list adapter about data changes
// so that it renders the list view with updated data
adapter.notifyDataSetChanged();
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String clicked_at = null;
try {
clicked_at = response.getString(position);
} catch (JSONException e) {
e.printStackTrace();
}
Intent i = new Intent(getApplicationContext(), full_view_walker.class);
i.putExtra("data", clicked_at);
i.putExtra("lat", finalLat1);
i.putExtra("lon", finalLon1);
i.putExtra("sender_id", sender_id);
startActivity(i);
}
});
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
hidePDialog();
}
});
// Adding request to request queue
AppController.getInstance().addToRequestQueue(movieReq);
}
现在我的标签内容:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.user_listview);
tabHost = (MaterialTabHost) this.findViewById(R.id.tabHost);
pager = (ViewPager) this.findViewById(R.id.pager);
// init view pager
adapter_page = new ViewPagerAdapter(getSupportFragmentManager());
pager.setAdapter(adapter_page);
pager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
// when user do a swipe the selected tab change
tabHost.setSelectedNavigationItem(position);
}
});
// insert all tabs from pagerAdapter data
for (int i = 0; i < adapter_page.getCount(); i++) {
tabHost.addTab(
tabHost.newTab()
.setText(adapter_page.getPageTitle(i))
.setTabListener(this)
);
}
@Override
public void onTabSelected(MaterialTab tab) {
pager.setCurrentItem(tab.getPosition()); }
private class ViewPagerAdapter extends FragmentStatePagerAdapter {
public ViewPagerAdapter(FragmentManager fm) {
super(fm);
}
public Fragment getItem(int num) {
return new FragmentText();
}
@Override
public int getCount() {
return 3;
}
private String[] lst = {"Distance", "Experience", "History"};
@Override
public CharSequence getPageTitle(int position) {
return lst[position];
}
}
我在这里有FragmentText:
public class FragmentText extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.front_list, container, false);
}
}
现在我真的需要两件事:
1)如何将FragmentText中的inflater.inflate连接到加载列表并填充它的代码? 2)如何确保每次用户切换标签时都不必重新加载,列表加载/排序一次然后从那里重新加载?
编辑:我在FragmentText中试过这个:
public class FragmentText extends Fragment{
// Log tag
private static final String TAG = AvailableList.class.getSimpleName();
String sender_id;
double distanceInMiles;
Double lat = null;
Double lon = null;
private ProgressDialog pDialog;
private List<viewer> movieList = new ArrayList<viewer>();
private ListView listView;
private CustomListAdapter adapter;
private String[] leftSliderData = {"Logout", "Contact Us"};
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
lon = getArguments().getDouble("lon");
lat = getArguments().getDouble("lat");
}
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View V = inflater.inflate(R.layout.front_list, container, false);
listView = (ListView) V.findViewById(R.id.list);
adapter = new CustomListAdapter(getActivity(), movieList);
Log.d("Adapter", adapter.toString()+"");
listView.setAdapter(adapter);
final Double finalLat = lat;
final Double finalLon = lon;
final Double finalLat1 = lat;
final Double finalLon1 = lon;
JsonArrayRequest movieReq = new JsonArrayRequest(url,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(final JSONArray response) {
Log.d(TAG, response.toString());
//hidePDialog();
for (int i = 0; i < response.length(); i++) {
try {
JSONObject obj = response.getJSONObject(i);
viewer movie = new viewer();
LatLng point1 = new LatLng(obj.getDouble("lat"), obj.getDouble("long"));
LatLng point2 = new LatLng(finalLat, finalLon);
distanceInMiles = LatLngTool.distance(point1, point2, LengthUnit.MILE);
movie.setTitle(obj.getString("first_name"));
movie.setThumbnailUrl(obj.getString("pic"));
movie.setYear(Double.parseDouble(String.format("%.2f", distanceInMiles)));
} catch (JSONException e) {
e.printStackTrace();
}
}
// notifying list adapter about data changes
// so that it renders the list view with updated data
adapter.notifyDataSetChanged();
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String clicked_at = null;
try {
clicked_at = response.getString(position);
} catch (JSONException e) {
e.printStackTrace();
}
Intent i = new Intent(getActivity(), full_view_walker.class);
i.putExtra("data", clicked_at);
i.putExtra("lat", finalLat1);
i.putExtra("lon", finalLon1);
i.putExtra("sender_id", sender_id);
startActivity(i);
}
});
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
//hidePDialog();
}
});
// Adding request to request queue
AppController.getInstance().addToRequestQueue(movieReq);
return V;
}
这是在我点击标签时加载页面,但它们没有显示!我可以看到列表的活动正在加载!
答案 0 :(得分:1)
1)如何将FragmentText中的inflater.inflate连接到代码 加载列表并填充它?
除了您发布的内容之外,没有什么特别的东西可以使用。
这是在我点击标签时加载页面,但它们没有显示!
第一个标签是否显示数据?
您使用包含相同片段的3个页面的ViewPager
,以便在所有页面上使用相同的布局。在许多情况下,ViewPager与页面内容具有相同的片段,您描述的行为就会发生,因为用户使用findViewById()直接在活动布局中查找各个页面的小部件。 findViewById()将在布局中返回该视图的第一次出现,因此您将始终从第一页获取视图,除了第0页之外的任何其他页面都不会显示任何内容,尽管数据加载(并在第一页上的小部件上设置)页面,没有显示)。但是,在最后的代码片段中,你在膨胀的布局中寻找ListView,所以这不是问题(在第一个使用简单的findViewById()查找ListView的代码片段中,不清楚?)所以你可能想看一下你的布局并检查它们是否存在问题。
另一个问题是,所有FragmentText是否加载相同的数据(“url”相同?)。如果是,那么你不应该在每个片段中进行网络调用(浪费),而是你应该有一个点进行网络调用和使用那个单点的片段(一个好的选项是没有用于setRetainInstance的UI片段) ()设置为true)。
如何确保每次用户切换时都不必重新加载 选项卡,列表加载/排序一次,然后只是 从那里重装?
与上述问题相同。这些3 FragmentText中的每一个都有相同的数据列表吗?如果是,那些选项卡只是根据某些标准(距离,经验等)对数据进行排序,而不是单个数据列表,并将标识符传递给FragmentText以了解应该在那里进行哪些排序。然后,当片段变得可见时(检查setUserVisibleHint()),根据该页面的条件对单个数据列表进行排序,并将其显示给用户。