How do I place fragment arrylist ScrollableTabsActivity?
I use rss feed. Lot of rss fragment for each url in them.How do I use 1 fragment lot of url array set. Most of these do not require a fragment and do not require a url I just need to
should use single fragment and pass the multiple title or multiple url as argument
Rssfragment
adapter.addFrag(new RssFragment(), "news");
URL rssUrl = new URL("http://www.rssmix.com/u/12847959/rss.xml");
How to set arry.how to joint ScrollableTabsActivity
Let me know if you know the answer to the question of how to compress all fragment
adapter.addFrag(new RssFragment(), "news");
adapter.addFrag(new RssFragment2(), "cinema news");
adapter.addFrag(new RssFragment3(), "news");
adapter.addFrag(new RssFragment4(), "news");
adapter.addFrag(new RssFragment5(), "news");
adapter.addFrag(new RssFragment6(), "news");
adapter.addFrag(new RssFragment7(), "news");
adapter.addFrag(new RssFragment8(), "news");
How to use 1 RssFragment array set url name:
This ScrollableTabsActivity These are the arrays to use
public class ScrollableTabsActivity extends AppCompatActivity {
private Toolbar toolbar;
private TabLayout tabLayout;
private ViewPager viewPager;
private AdView mAdView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scrollable_tabs);
mAdView = findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
toolbar = (Toolbar) findViewById(R.id.toolbar2);
setSupportActionBar(toolbar);
// getSupportActionBar().setDisplayHomeAsUpEnabled(true);
viewPager = (ViewPager) findViewById(R.id.viewpager);
setupViewPager(viewPager);
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFrag(new RssFragment(), "news");
adapter.addFrag(new RssFragment2(), "news");
adapter.addFrag(new RssFragment3(), "news");
adapter.addFrag(new RssFragment4(), "news");
adapter.addFrag(new RssFragment5(), "news");
adapter.addFrag(new RssFragment6(), "news");
adapter.addFrag(new RssFragment7(), "news");
adapter.addFrag(new RssFragment8(), "news");
viewPager.setAdapter(adapter);
}
@Override
public void onBackPressed() {
new AlertDialog.Builder(this).setIcon(android.R.drawable.ic_dialog_alert).setTitle("Exit")
.setMessage("Are you sure you want to exit?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
}).setNegativeButton("No", null).show();
}
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
@Override
public int getCount() {
return mFragmentList.size();
}
public void addFrag(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
}
Rssfragment
public class RssFragment extends Fragment {
private RSSFeed rssFeed = null;
private ArrayList<RSSItem> postsList;
private RssAdapter listAdapter;
private ViewModeUtils viewModeUtils;
private SwipeRefreshLayout swipeRefreshLayout;
private Activity mAct;
private RelativeLayout ll;
private String url;
private AdView mAdView;
private View rootView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
ll = (RelativeLayout) inflater.inflate(R.layout.fragment_list_refresh, container, false);
return ll;
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
setHasOptionsMenu(true);
RecyclerView listView = ll.findViewById(R.id.list);
postsList = new ArrayList<>();
listAdapter = new RssAdapter(getContext(), postsList);
listAdapter.setModeAndNotify(InfiniteRecyclerViewAdapter.MODE_PROGRESS);
listView.setAdapter(listAdapter);
listView.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false));
swipeRefreshLayout = ll.findViewById(R.id.swipeRefreshLayout);
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
refreshItems();
}
});
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mAct = getActivity();
// url = RssFragment.this.getArguments().getStringArray(MainActivity.FRAGMENT_DATA)[0];
refreshItems();
}
private class RssTask extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... arg0) {
try {
URL rssUrl = new URL("http://www.rssmix.com/u/12847959/rss.xml");
SAXParserFactory mySAXParserFactory = SAXParserFactory.newInstance();
SAXParser mySAXParser = mySAXParserFactory.newSAXParser();
XMLReader myXMLReader = mySAXParser.getXMLReader();
RSSHandler myRSSHandler = new RSSHandler();
myXMLReader.setContentHandler(myRSSHandler);
InputSource myInputSource = new InputSource(rssUrl.openStream());
myXMLReader.parse(myInputSource);
rssFeed = myRSSHandler.getFeed();
} catch (ParserConfigurationException | IOException | SAXException e) {
Log.printStackTrace(e);
}
return null;
}
@Override
protected void onPostExecute(Void result) {
if (rssFeed != null) {
if (rssFeed.getList().size() > 0) {
postsList.addAll(rssFeed.getList());
}
listAdapter.setHasMore(false);
listAdapter.setModeAndNotify(InfiniteRecyclerViewAdapter.MODE_LIST);
swipeRefreshLayout.setRefreshing(false);
} else {
String message = null;
if (!url.startsWith("http"))
message = "Debug info: '" + url + "' is most likely not a valid RSS url. Make sure the url entered in your configuration starts with 'http' and verify if it's valid XML using https://validator.w3.org/feed/";
Helper.noConnection(mAct, message);
listAdapter.setModeAndNotify(InfiniteRecyclerViewAdapter.MODE_EMPTY);
swipeRefreshLayout.setRefreshing(false);
}
super.onPostExecute(result);
}
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.rss_menu, menu);
viewModeUtils = new ViewModeUtils(getContext(), getClass());
viewModeUtils.inflateOptionsMenu(menu, inflater);
ThemeUtils.tintAllIcons(menu, mAct);
}
private void refreshItems() {
postsList.clear();
listAdapter.setModeAndNotify(InfiniteRecyclerViewAdapter.MODE_PROGRESS);
new RssTask().execute();
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
viewModeUtils.handleSelection(item, new ViewModeUtils.ChangeListener() {
@Override
public void modeChanged() {
listAdapter.notifyDataSetChanged();
}
});
switch (item.getItemId()) {
case R.id.info:
//show information about the feed in general in a dialog
if (rssFeed != null) {
String FeedTitle = (rssFeed.getTitle());
String FeedDescription = (rssFeed.getDescription());
//String FeedPubdate = (myRssFeed.getPubdate()); most times not present
String FeedLink = (rssFeed.getLink());
AlertDialog.Builder builder = new AlertDialog.Builder(mAct);
String titlevalue = getResources().getString(R.string.feed_title_value);
String descriptionvalue = getResources().getString(R.string.feed_description_value);
String linkvalue = getResources().getString(R.string.feed_link_value);
if (FeedLink.equals("")) {
builder.setMessage(titlevalue + ": \n" + FeedTitle +
"\n\n" + descriptionvalue + ": \n" + FeedDescription);
} else {
builder.setMessage(titlevalue + ": \n" + FeedTitle +
"\n\n" + descriptionvalue + ": \n" + FeedDescription +
"\n\n" + linkvalue + ": \n" + FeedLink);
}
builder.setNegativeButton(getResources().getString(R.string.ok), null)
.setCancelable(true);
builder.create();
builder.show();
}
return true;
default:
return super.onOptionsItemSelected(item);
}
}
activity_scrollable_tabs
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:ads="http://schemas.android.com/tools"
android:id="@+id/htab_maincontent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="scrollable"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar2"
app:tabMode="scrollable"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
<com.google.android.material.tabs.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="scrollable"/>
</com.google.android.material.appbar.AppBarLayout>
<androidx.viewpager.widget.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
/>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
I am updating my answer which solved your issue.
I think what are you trying to achieve is the reusability of your RssFragment
.
You need to add a new instance of your RssFragment
to your adapter by passing different URLs as arguments to it.
I have created a static method in your RssFragment class so you can create an instance with a URL argument. Refer to the code below:
public class RssFragment extends Fragment {
// Added argument key for URL
private static final String ARG_URL = "url_string";
private RSSFeed rssFeed = null;
private ArrayList<RSSItem> postsList;
private RssAdapter listAdapter;
private ViewModeUtils viewModeUtils;
private SwipeRefreshLayout swipeRefreshLayout;
private Activity mAct;
private RelativeLayout ll;
private String URL;
private AdView mAdView;
private View rootView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
ll = (RelativeLayout) inflater.inflate(R.layout.fragment_list_refresh, container, false);
return ll;
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
setHasOptionsMenu(true);
RecyclerView listView = ll.findViewById(R.id.list);
postsList = new ArrayList<>();
listAdapter = new RssAdapter(getContext(), postsList);
listAdapter.setModeAndNotify(InfiniteRecyclerViewAdapter.MODE_PROGRESS);
listView.setAdapter(listAdapter);
listView.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false));
swipeRefreshLayout = ll.findViewById(R.id.swipeRefreshLayout);
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
refreshItems();
}
});
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mAct = getActivity();
// get URL from arguments
url = RssFragment.this.getArguments().getString(ARG_URL);
refreshItems();
}
private class RssTask extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... arg0) {
try {
// Pass the URL string as parameter to URL class
URL rssUrl = new URL(URL);
SAXParserFactory mySAXParserFactory = SAXParserFactory.newInstance();
SAXParser mySAXParser = mySAXParserFactory.newSAXParser();
XMLReader myXMLReader = mySAXParser.getXMLReader();
RSSHandler myRSSHandler = new RSSHandler();
myXMLReader.setContentHandler(myRSSHandler);
InputSource myInputSource = new InputSource(rssUrl.openStream());
myXMLReader.parse(myInputSource);
rssFeed = myRSSHandler.getFeed();
} catch (ParserConfigurationException | IOException | SAXException e) {
Log.printStackTrace(e);
}
return null;
}
@Override
protected void onPostExecute(Void result) {
if (rssFeed != null) {
if (rssFeed.getList().size() > 0) {
postsList.addAll(rssFeed.getList());
}
listAdapter.setHasMore(false);
listAdapter.setModeAndNotify(InfiniteRecyclerViewAdapter.MODE_LIST);
swipeRefreshLayout.setRefreshing(false);
} else {
String message = null;
if (!url.startsWith("http"))
message = "Debug info: '" + url + "' is most likely not a valid RSS url. Make sure the url entered in your configuration starts with 'http' and verify if it's valid XML using https://validator.w3.org/feed/";
Helper.noConnection(mAct, message);
listAdapter.setModeAndNotify(InfiniteRecyclerViewAdapter.MODE_EMPTY);
swipeRefreshLayout.setRefreshing(false);
}
super.onPostExecute(result);
}
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.rss_menu, menu);
viewModeUtils = new ViewModeUtils(getContext(), getClass());
viewModeUtils.inflateOptionsMenu(menu, inflater);
ThemeUtils.tintAllIcons(menu, mAct);
}
private void refreshItems() {
postsList.clear();
listAdapter.setModeAndNotify(InfiniteRecyclerViewAdapter.MODE_PROGRESS);
new RssTask().execute();
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
viewModeUtils.handleSelection(item, new ViewModeUtils.ChangeListener() {
@Override
public void modeChanged() {
listAdapter.notifyDataSetChanged();
}
});
switch (item.getItemId()) {
case R.id.info:
//show information about the feed in general in a dialog
if (rssFeed != null) {
String FeedTitle = (rssFeed.getTitle());
String FeedDescription = (rssFeed.getDescription());
//String FeedPubdate = (myRssFeed.getPubdate()); most times not present
String FeedLink = (rssFeed.getLink());
AlertDialog.Builder builder = new AlertDialog.Builder(mAct);
String titlevalue = getResources().getString(R.string.feed_title_value);
String descriptionvalue = getResources().getString(R.string.feed_description_value);
String linkvalue = getResources().getString(R.string.feed_link_value);
if (FeedLink.equals("")) {
builder.setMessage(titlevalue + ": \n" + FeedTitle +
"\n\n" + descriptionvalue + ": \n" + FeedDescription);
} else {
builder.setMessage(titlevalue + ": \n" + FeedTitle +
"\n\n" + descriptionvalue + ": \n" + FeedDescription +
"\n\n" + linkvalue + ": \n" + FeedLink);
}
builder.setNegativeButton(getResources().getString(R.string.ok), null)
.setCancelable(true);
builder.create();
builder.show();
}
return true;
default:
return super.onOptionsItemSelected(item);
}
}
// This is the factory to instantiate the RssFragment instance with the url string as arguments
public static RssFragment newInstance(String url) {
RssFragment newInstance = new RssFragment();
Bundle bundle = new Bundle();
bundle.putString(ARG_URL, URL);
newInstance.setArguments(bundle);
return newInstance;
}
}
Now you can reuse the RssFragment
class in your ScrollableTabsActivity
instead of creating fragment classes for every URL string like the below code:
adapter.addFrag(RssFragment.newInstance("YOUR_URL"), "news");
adapter.addFrag(RssFragment.newInstance("YOUR_URL"), "cinema news");
adapter.addFrag(RssFragment.newInstance("YOUR_URL"), "news");
adapter.addFrag(RssFragment.newInstance("YOUR_URL"), "news");
adapter.addFrag(RssFragment.newInstance("YOUR_URL"), "news");
adapter.addFrag(RssFragment.newInstance("YOUR_URL"), "news");
adapter.addFrag(RssFragment.newInstance("YOUR_URL"), "news");
adapter.addFrag(RssFragment.newInstance("YOUR_URL"), "news");