How to Place Fragment Arrylist Scrollabletabsactivity

How do I place fragment arrylist ScrollableTabsActivity?

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");

setting up scrolling fragments into Scrolling Activity

You can simply add ViewPager to content_layout with TabLayout. You can simply use this tutorial for tabs and ViewPager.

Also add android:fillViewport="true" in NestedScrollView.

Android: How to make a scroll-able list of fragments

So here is what I found best if you want a smooth scroll between the list of fragments (that means you don't want a view pager). It's got a fair bit of jank but it worked fine in my case.

I had enough framelayouts to cover as many fragments as I'd ever need. I then set them to the fragments I needed.

private void LoadFragments() {
mainActiveTools = loadActiveTools();
frameLayouts = loadFrameLayouts();
fragments = loadFragments();

android.support.v4.app.FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

int i = 0;
while(i != frameLayouts.size()) {
int currentFrame = frameLayouts.get(i);
try {
android.support.v4.app.Fragment currentFrag = fragments.get(i);
transaction.replace(currentFrame, currentFrag);
findViewById(currentFrame).setVisibility(View.VISIBLE);
}
catch (IndexOutOfBoundsException e) {
findViewById(currentFrame).setVisibility(View.INVISIBLE);
}
i++;
}
transaction.commit();
}

This code attempts to fill out each of my 5 framelayouts stored in said ArrayList. A transaction is instantiated, and then a while loop gives each of the framelayouts a fragment. Additionally, each framelayout is also explicitly told whether or not to be visible.

Now I'm a Java/Android programming beginner, but at least for me, this did the job. No idea how scalable this is (my guess would be until you get bored of copy-pasting or the app crashes) nor how performance heavy this is NOR whether or not it will even work for you!

Scrolling in Fragment doesn't work into a TabLayout

You have put the viewpager code inside AppBarLayout in activity_main.xml. Cut and Paste the ViewPager outside AppBarLayout. Copy this code in your activity_main.xml. Hope it solves your problem.

<?xml version="1.0" encoding="utf-8"?>

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
app:tabIndicatorColor="@color/colorPrimary"
app:pstsIndicatorHeight="3dip"
app:pstsTextAllCaps="false"
app:tabPaddingStart="2dp"
app:tabPaddingEnd="2dp"
app:tabGravity="fill"/>


</android.support.design.widget.AppBarLayout>

<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />

</android.support.design.widget.CoordinatorLayout>

listview in fragment doesn't scroll

Try add this following piece of code:

listView.setOnTouchListener(new ListView.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
int action = event.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
events.
v.getParent().requestDisallowInterceptTouchEvent(true);
break;

case MotionEvent.ACTION_UP:
events.
v.getParent().requestDisallowInterceptTouchEvent(false);
break;
}


v.onTouchEvent(event);
return true;
}
});

Explanation, Android has not a great support for nested scrolls, so you need to disable the "Parent" touch management and pass the event to your view.

Hope this help

How do I get a specific Activity to show up in a Scrollable Tabs?

You are a newbie, so I'll try to explain how the sample project works.
First of all, you talk about Activity, but you need to use Fragment. The activity is just the host for one or more fragments.

The activity diplays only a ViewPager which is used to display some Fragment (or a View) and enable swiping from one to another.
The SectionsPagerAdapter works like all Adapter objects in Android, you create one, you give it some data (ie : ArrayList of country), you implement some methods including the getView or in this case getItem method. In these method you are given the position in the ViewPager, so you can access the right data and give it to your Fragment via a Bundle object (it's done in this example, the position +1).

To give data, use this :

            VisuFragment fragment = new VisuFragment();
Bundle args = new Bundle();
args.putInt("id", id);
fragment.setArguments(args);
return fragment;

And to get it from the onCreate method of the Fragment :

            int id = getArguments().getInt("id");

The ViewPager and the Adpater will manage everything on the screen, it will ask for new Fragment and delete old ones without you knowing.

For the sample code, if you want to see something different on each page, delete the all content of the onCreateView method and put this :

            TextView tv = new TextView(getActivity().getApplicationContext());
tv.setText("This is fragment number : "+getArguments().getInt(DummySectionFragment.ARG_SECTION_NUMBER);
return tv;


Related Topics



Leave a reply



Submit