我有屏幕Book,如果要在屏幕上重新加载数据,我想添加FloatingActionButton,我必须单击该按钮。我在Book_Fragment中添加了notifydatasetchanged来检查更改。但是单击后,按钮将重新加载列表视图不变的数据。
我不知道该如何解决。请帮我。谢谢
这是我的源代码:
Book_Fragment.java
public class Book_Fragment extends Fragment {
String urlJsonArry = "https://book.com/json.json";
Constants constants;
ListView listView;
ListAdapter_Book listAdapter_Book;
public ArrayList<Constants> book_data_list = new ArrayList<Constants>();
View rootView;
public Book_Fragment() {}
@Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.book, container, false);
listView = (ListView)rootView.findViewById(R.id.data);
listAdapter_Book = new ListAdapter_Book(this);
listView.setAdapter(listAdapter_Book);
listView.setDivider(null);
book_data_list.clear();
getData();
return rootView;
}
private void getData() {
String tag_string_req1 = "request";
StringRequest strReq1 = new StringRequest(Request.Method.GET, urlJsonArry, new Response.Listener<String>() {
@Override public void onResponse(String response) {
try {
JSONObject jsonObject_main = new JSONObject(response);
JSONArray stories = jsonObject_main.getJSONArray("data");
for (int i = 0; i < stories.length(); i ++) {
constants = new Constants();
JSONObject jsonObject = stories.getJSONObject(i);
constants.book_id = jsonObject.getString("id");
constants.book_name = jsonObject.getString("book_name");
book_data_list.add(constants);
}
book_data_list.notifyDataSetChanged();
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(getContext(), "Error: " + e.getMessage(), Toast.LENGTH_LONG).show();
}
pDialog.hide();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
pDialog.hide();
}
}) {};
AppController.getInstance().addToRequestQueue(strReq1, tag_string_req1);
}
}
Mainactivity.java
public class MainActivity extends AppCompatActivity implements FragmentDrawer.FragmentDrawerListener {
public ViewPager viewPager;
private Toolbar mToolbar;
FrameLayout ly;
private FragmentDrawer drawerFragment;
private FloatingActionButton floatingbutton;
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mToolbar = (Toolbar)findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
ly = (FrameLayout)findViewById(R.id.container_body);
ly.setVisibility(View.GONE);
floatingbutton = (FloatingActionButton)findViewById(R.id.fab);
final TabLayout tabLayout = (TabLayout)findViewById(R.id.tab_layout);
tabLayout.addTab(tabLayout.newTab().setText("Book"));
getSupportActionBar().setElevation(0);
tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
viewPager = (ViewPager)findViewById(R.id.pager);
final PagerAdapter adapter = new PagerAdapter(getSupportFragmentManager(), tabLayout.getTabCount());
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
viewPager.setVisibility(View.VISIBLE);
ly.setVisibility(View.GONE);
viewPager.setCurrentItem(tab.getPosition());
if (tab.getPosition() == 0) {
mToolbar.setTitle("Book");
}
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {}
@Override
public void onTabReselected(TabLayout.Tab tab) {}
});
floatingbutton.setOnClickListener(new View.OnClickListener() {
Fragment fragment = null;
@Override
public void onClick(View v) {
int current_tab = viewPager.getCurrentItem();
switch (current_tab) {
case 0:
fragment = new Book_Fragment();
break;
default:
break;
}
if (fragment != null) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.container_body, fragment);
fragmentTransaction.commit();
}
}
});
drawerFragment = (FragmentDrawer)getSupportFragmentManager().findFragmentById(R.id.fragment_navigation_drawer);
drawerFragment.setUp(R.id.fragment_navigation_drawer, (DrawerLayout)findViewById(R.id.drawer_layout), mToolbar);
drawerFragment.setDrawerListener(this);
displayView(0);
}
@Override public void onDrawerItemSelected(View view, int position) {
displayView(position);
ly.setVisibility(View.GONE);
viewPager.setVisibility(View.VISIBLE);
}
private void displayView(int position) {
switch (position) {
case 0:
viewPager.setCurrentItem(0);
break;
default:
break;
}
}
}
ListAdapter_Book.java
public class ListAdapter_Book extends BaseAdapter {
Book_Fragment main;
public ListAdapter_Book(Book_Fragment main) {
this.main = main;
}
@Override public int getCount() {
return main.book_data_list.size();
}
@Override public Object getItem(int position) {
return null;
}
@Override public long getItemId(int position) {
return 0;
}
public static class ViewHolderItem {
public TextView book_name;
}
@Override public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolderItem holder = new ViewHolderItem();
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater)main.getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.book_list_cell, null);
holder.book_name = (TextView)convertView.findViewById(R.id.book_name);
holder.book_name = this.main.book_data_list.get(position).data_book_name;
} else {
holder = (ViewHolderItem)convertView.getTag();
}
final ViewHolderItem finalHolder = holder;
convertView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent opn = new Intent(main.getContext(), Book_Details.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
opn.putExtra("id", main.book_data_list.get(position).data_id);
main.getContext().startActivity(opn);
}
});
return convertView;
}
}
最佳答案
您的适配器应该是这样的:
public class ListAdapter_Book extends BaseAdapter {
Book_Fragment main;
List<Constants> book_data_list=new ArrayList<>();
public ListAdapter_Book(Book_Fragment main) {
this.main = main;
}
@Override
public int getCount() {
return book_data_list.size();
}
@Override
public Object getItem(int position) {
return book_data_list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
public static class ViewHolderItem {
public TextView book_name;
}
public void addItems(List<Constant> items){
book_data_list.addAll(items);
notifyDataSetChanged();
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolderItem holder = new ViewHolderItem();
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) main.getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.book_list_cell, null);
holder.book_name = (TextView) convertView.findViewById(R.id.book_name);
holder.book_name = this.main.book_data_list.get(position).data_book_name;
} else {
holder = (ViewHolderItem) convertView.getTag();
}
final ViewHolderItem finalHolder = holder;
convertView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent opn = new Intent(main.getContext(), Book_Details.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
opn.putExtra("id", main.book_data_list.get(position).data_id);
main.getContext().startActivity(opn);
}
});
return convertView;
}
}
现在在您进行网络呼叫的片段中:
public class Book_Fragment extends Fragment {
String urlJsonArry = "https://book.com/json.json";
Constants constants;
ListView listView;
ListAdapter_Book listAdapter_Book;
public ArrayList<Constants> book_data_list = new ArrayList<Constants>();
View rootView;
public Book_Fragment() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.book, container, false);
listView = (ListView) rootView.findViewById(R.id.data);
listAdapter_Book = new ListAdapter_Book(this);
listView.setAdapter(listAdapter_Book);
listView.setDivider(null);
getData();
return rootView;
}
private void getData() {
String tag_string_req1 = "request";
StringRequest strReq1 = new StringRequest(Request.Method.GET,
urlJsonArry, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jsonObject_main = new JSONObject(response);
JSONArray stories = jsonObject_main.getJSONArray("data");
for (int i = 0; i < stories.length(); i++) {
constants = new Constants();
JSONObject jsonObject = stories.getJSONObject(i);
constants.book_id = jsonObject.getString("id");
constants.book_name = jsonObject.getString("book_name");
book_data_list.add(constants);
}
listAdapter_Book.addItems(book_data_list);
} catch (
JSONException e
) {
e.printStackTrace();
Toast.makeText(getContext(),
"Error: " + e.getMessage(),
Toast.LENGTH_LONG).show();
}
pDialog.hide();
}
}
, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
pDialog.hide();
}
}
) {
};
AppController.getInstance().
addToRequestQueue(strReq1, tag_string_req1);
}
}
关于java - 如何使用notifyDataSetChanged和Volley,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57198317/