I have a ViewPager that contains several instances of the same fragment, this fragment contains an article. The Article view hierarchy is quite simple, a Title, a Banner image, a subtitle and a body; everything but the title is wrapped in a scrollview.
The problem is, when you swipe to a new page, the fragment is presented with the Views at the top, and then it immediately scrolls to the middle of the container. (As a matter of fact it scrolls to the beginning of the TextView with id: article_content)
I have posted the layout at the bottom of the question.
Now, the ViewPager is set with a simple implementation of a FragmentStatePagerAdapter, here's the code:
class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
Bundle args;
int count;
public ScreenSlidePagerAdapter(FragmentManager fm) {
super(fm);
this.count = 8;
}
#Override
public Fragment getItem(int position) {
ArticleFragment mPrimaryFragment = new ArticleFragment();
args = new Bundle();
args.putString(ArticleFragment.KEY_ARTICLE_URL, mCurArticleLink);
mPrimaryFragment.setArguments(args);
return mPrimaryFragment;
}
#Override
public int getCount() {
return count;
}
}
The Fragment itself is pretty simple too. First, I check during onCreate to see if we have the article cached, the I call on onCreateView
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.apk_article_view, null);
mTitle = (TextView) view.findViewById(R.id.article_title);
mBanner = (ImageView) view.findViewById(R.id.article_banner);
mSubTitle = (TextView) view.findViewById(R.id.article_subtitle);
mContent = (TextView) view.findViewById(R.id.article_content);
if (isArticleCached) {
Constants.logMessage("Article is cached, loading from database");
setApkArticleContent();
}
else {
Constants.logMessage("Article isn't cached, downloading");
HtmlHelper.setApkArticleContent(mContext, mUrl, mTitle, mSubTitle, mContent, mBanner);
setRefreshing(true);
}
return view;
}
It is worth noting that setApkArticleContent is a simple set of Texts, nothing fancy:
private void setApkArticleContent() {
mTitle.setText(Html.fromHtml(mCursor.getString(mCursor.getColumnIndex(DbOpenHelper.TITLE))));
mSubTitle.setText(Html.fromHtml(mCursor.getString(mCursor.getColumnIndex(DbOpenHelper.SUBTITLE))));
mContent.setText(Html.fromHtml(mCursor.getString(mCursor.getColumnIndex(DbOpenHelper.BODY))));
UrlImageViewHelper.setUrlDrawable(mBanner, mCursor.getString(mCursor.getColumnIndex(DbOpenHelper.BANNER)));
}
Also, please know that I did not have a pager before, the fragment was only loaded to an empty activity, and it worked without scrolling to the middle of the scrollview.
I am really not sure what is triggering the scroll, and yes, I know I can programatically set it to scroll back to the top after loading, but then again, that'd be two scroll movements when the fragment is loaded and it would be quite noticeable for the user.
Do you guys have any ideas why it would behave like this? Any ideas on how I can stop that unintentional scroll?
Thanks,
Below is the layout for the ArticleFragment:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/article_title"
style="#style/headerTextBoldNoUnderline"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="left|center_vertical"
android:text="" />
<ScrollView
android:layout_height="match_parent"
android:layout_width="match_parent"
>
<LinearLayout
android:orientation="vertical"
android:layout_height="wrap_content"
android:layout_width="match_parent"
>
<ImageView
android:id="#+id/article_banner"
android:layout_gravity="center_vertical|center_horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="12dp" />
<TextView
android:id="#+id/article_subtitle"
style="#style/HeaderTextItalicNoUnderline"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="left|center_vertical" />
<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="?android:attr/dividerVertical" />
<TextView
android:id="#+id/article_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-light"
android:gravity="left|center_vertical"
android:padding="8dp"
android:textColor="?android:attr/textColorSecondary"
android:textIsSelectable="true"
android:textSize="16sp" />
</LinearLayout>
</ScrollView>
</LinearLayout>
This is likely caused by android:textIsSelectable. You may try adding the following to the ScrollView:
android:focusable="true"
android:focusableInTouchMode="true"
android:descendantFocusability="beforeDescendants"
Add this attribute value android:descendantFocusability="blocksDescendants" to the child of ScrollView, according to this question: How to prevent a scrollview from scrolling to a webview after data is loaded?
I also had this problem. I solved it with setting android:focusable="true" and android:focusableInTouchMode="true" to the first element in the ScrollView's LinearLayout.
i try to add
android:focusable="true"
android:focusableInTouchMode="true"
android:descendantFocusability="beforeDescendants"
in the root viewGroup .
it's work's well.
like below.
<RelativeLayout 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:focusable="true"
android:focusableInTouchMode="true"
android:descendantFocusability="beforeDescendants"
android:background="#color/normal_bg">
<ScrollView
android:id="#+id/scroll"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/rl_enter"
android:scrollbars="none">
</ScrollView>
</RelativeLayout>
I tried solution:
android:focusable=true
android:focusableInTouchMode=true
android:descendantFocusability=beforeDescendants
And i dont know why but I cant do this with my RelativeLayout which is parent view (this does not work).
I had to wrap my TextView inside FrameLayout and now everything is ok. Maybe this helps somebody.
<FrameLayout
android:id="#+id/detail_description_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/detail_parameters_container"
android:layout_centerInParent="true"
android:descendantFocusability="beforeDescendants"
android:focusable="true"
android:focusableInTouchMode="true" >
<TextView
android:id="#+id/detail_descritpion"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:autoLink="all"
android:padding="10dp"
android:textIsSelectable="true" />
</FrameLayout>
ScrollView inside ViewPager scrolls automatically but does not scrolls in middle..
Check out this.
pager.setOnPageChangeListener(new OnPageChangeListener() {
#Override
public void onPageSelected(int position) {
// TODO Auto-generated method stub
int x=iv.getLeft();
int y=iv.getTop();
scroll.scrollTo(x, y);
}
pager is ViewPager's object and scroll is ScrollView and iv is any View say TextView or ImageView.
When we change page in viewpager, scrollbar automatically scrolls but to the left. If you would have find solution to middle please let me know.. :)
Related
I know there are several questions for this problem already as I have searched for an hour, but neither of those has solved my problem nor the solution is too old.
I'm working on an event app and it displays the attendance of people going to it. It need to has two GridView: YES and NO, depending on the attendance, and it shows the picture of the guests.
I have already made the "YES" GridView and I tried adding a new TextView and GridView to my layout + getting the layout of it in Java for my "NO" GridView but it only shows the first one.
What am I doing wrong? I'm using Fragments for each of my tabs.
This is what I have v.s. what I want:
This is my fragment's layout:
[fragment_one.xml]
<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"
tools:context="com.example.example.MainActivity.OneFragment">
<TextView android:id="#+id/title_yes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="14dp"
android:text="Yes"
android:textAllCaps="true"
android:textSize="14sp"
android:fontFamily="sans-serif-regular" />
<GridView android:id="#+id/gridview_yes"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/title_yes"
android:numColumns="4"
android:verticalSpacing="2dp"
android:horizontalSpacing="2dp"
android:stretchMode="columnWidth"
android:gravity="center" />
<TextView android:id="#+id/title_no"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="14dp"
android:text="No"
android:textAllCaps="true"
android:textSize="14sp"
android:fontFamily="sans-serif-regular" />
<GridView android:id="#+id/gridview_no"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/title_no"
android:numColumns="4"
android:verticalSpacing="2dp"
android:horizontalSpacing="2dp"
android:stretchMode="columnWidth"
android:gravity="center" />
</LinearLayout>
This is my fragment's Java:
public class OneFragment extends Fragment {
public OneFragment() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_one, container, false);
GridView gridViewYes = (GridView) view.findViewById(R.id.gridview_yes);
GridView gridViewNo = (GridView) view.findViewById(R.id.gridview_no);
gridViewYes.setAdapter(new ImageAdapter(view.getContext())); // uses the view to get the context instead of getActivity()
gridViewNo.setAdapter(new ImageAdapter(view.getContext())); // uses the view to get the context instead of getActivity()
gridViewYes.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Toast.makeText(getActivity(), "Esta es la imagen " + position + ".", Toast.LENGTH_SHORT).show();
}
});
return view;
}
}
GridView is probably not your best option here. That widget is intended to be used when you have a potentially long list of things to display in a scrolling container. TableView is better when you have a manageable set of items that doesn't need its own scrolling behavior.
Also, you probably don't want a layout_height of match_parent for these things. Use wrap_content to only let them be as tall as their content suggests.
If you need the whole thing to scroll because you aren't certain of the entire height, place the LinearLayout in a ScrollView and let that determine if the collection of tables and text should be scrollable if they're too tall for the space allotted.
Your problem is you have set GridView yes height to match_parent.
android:layout_height="match_parent"
You should change as follows:
android:layout_height="wrap_content"
This question already has answers here:
RecyclerView items with big empty space after 23.2.0
(5 answers)
Closed 3 months ago.
I'm making a ToDo list app, and while testing it, for some reason, a huge gap forms between the items whenever I try to scroll down. It always happens whenever I Drag and Drop the items, but I don't see any errors with my ItemTouchHelper adapter and callback class. It would be awesome if you can help me out.
Before:
After:
RecyclerAdapter.java
public class RecyclerAdapter extends
RecyclerView.Adapter<RecyclerAdapter.RecyclerVH> implements ItemTouchHelperAdapter{
private LayoutInflater layoutInflater;
ArrayList<Info> data;
Context context;
public RecyclerAdapter(Context context) {
layoutInflater = LayoutInflater.from(context);
this.context = context;
}
public void setData(ArrayList<Info> data) {
this.data = data;
notifyDataSetChanged();
}
#Override
public RecyclerVH onCreateViewHolder(ViewGroup viewGroup, int position) {
View view = layoutInflater.inflate(R.layout.custom_row, viewGroup, false);
view.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d("R.A onClick Owen", "onClick method triggered");
}
});
RecyclerVH recyclerVH = new RecyclerVH(view);
return recyclerVH;
}
#Override
public void onBindViewHolder(RecyclerVH recyclerVH, int position) {
Log.d("RecyclerView", "onBindVH called: " + position);
final Info currentObject = data.get(position);
// Current Info object retrieved for current RecyclerView item - USED FOR DELETE
recyclerVH.listTitle.setText(currentObject.title);
recyclerVH.listContent.setText(currentObject.content);
/*recyclerVH.listTitle.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Open new Activity containing note content
Toast.makeText(this, "Opening: " + currentObject.title, Toast.LENGTH_LONG).show();
}
});*/
}
public void deleteItem(int position) {
DBInfo dbInfo = new DBInfo(context);
dbInfo.deleteNote(data.get(position));
// Deletes RV item/position's Info object
data.remove(position);
// Removes Info object at specified position
notifyItemRemoved(position);
// Notifies the RV that item has been removed
}
#Override
public int getItemCount() {
return data.size();
}
// This is where the Swipe and Drag-And-Drog methods come into place
#Override
public boolean onItemMove(int fromPosition, int toPosition) {
// Swapping positions
// ATTEMPT TO UNDERSTAND WHAT IS GOING ON HERE
Collections.swap(data, fromPosition, toPosition);
notifyItemMoved(fromPosition, toPosition);
return true;
}
#Override
public void onItemDismiss(int position) {
// Deleting item from RV and DB
deleteItem(position);
}
class RecyclerVH extends RecyclerView.ViewHolder implements View.OnClickListener{
// OnClickListener is implemented here
// Can also be added at onBindViewHolder above
TextView listTitle, listContent;
public RecyclerVH(View itemView) {
super(itemView);
listTitle = (TextView) itemView.findViewById(R.id.title);
listContent = (TextView) itemView.findViewById(R.id.content);
listTitle.setOnClickListener(this);
}
#Override
public void onClick(View v) {
Toast.makeText(context, "Opening: Note" + getLayoutPosition(), Toast.LENGTH_SHORT).show();
// PS NEVER ADD listTitle VARIABLE AS PUBLIC VARIABLE ABOVE WHICH IS GIVEN VALUE AT ONBINDVH
// THIS IS BECAUSE THE VALUE WILL CHANGE IF ITEM IS ADDED OR DELETED
}
}
}
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:fab="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical"
android:weightSum="1">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/rounded_corners" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerList"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.melnykov.fab.FloatingActionButton
android:id="#+id/fab_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginBottom="16dp"
android:layout_marginRight="16dp"
android:layout_marginEnd="16dp"
android:gravity="bottom|end"
android:onClick="addNote"
android:src="#drawable/fab_ic_add"
fab:fab_colorNormal="#color/colorPrimary"
fab:fab_colorPressed="#color/colorPrimaryDark"
fab:fab_colorRipple="#color/colorPrimaryDark" />
</RelativeLayout>
</FrameLayout>
</LinearLayout>
custom_row.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/main"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="#+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:padding="8dp"
android:text="#string/test"
android:textSize="18sp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/main"
android:paddingLeft="8dp">
<TextView
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/test"
android:textSize="15sp" />
</LinearLayout>
</RelativeLayout>
Thank you so much to whoever can help me out. I am pulling my hair out as I type.
EDIT: I have confirmed that it is not my ItemTouchHelper class that's the problem. (Tried running without it being called, problem still occurs.)
Also, it seems that when a dialog is shown and the keyboard brought up, the RecyclerView in the background resolves the problem by itself. After dialog is removed, the problem repeats (i.e. Scrolling puts massive space between items)
change in Recycler view match_parent to wrap_content:
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
Also change in item layout xml
Make parent layout height match_parent to wrap_content
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
This happened to me many times!
All you need to do is... make layout_height of row file wrap_content and your problem solved..
android:layout_height="wrap_content"
Happy coding!
Its because you are using match_parent in height of root view of the row item in your vertically oriented listview/recyclerview. When you use that the item expands completely wrt to its parent.
Use wrap_content for height when the recyclerview is vertically oriented and for width when it is horizantally oriented.
Avoid taking the view in item layout with a container like this:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data/>
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?android:listPreferredItemHeight"
android:textAppearance="?android:attr/textAppearanceMedium" />
</android.support.constraint.ConstraintLayout>
</layout>
as in this case match_parent will do its work and the problem will still remain! Rather take it like this:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data/>
<TextView
android:id="#+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?android:listPreferredItemHeight"
android:textAppearance="?android:attr/textAppearanceMedium" />
</layout>
[Note: Above code has data binding attached to it, don't use <layout> & <data> tags if not using data binding]
Other than this, if you must use any view group containers, than take a look the height and width attributes in the container, and try change those from match_parent to wrap_content. That should resolve the issue. For more transparency, one can try giving background colours and see through it to identify actual problem.
Just for the record: Since in my case we had to implement some "superfancy UI" with overlapping RecyclerView and blur effect toolbar, I had to get rid of clipToPadding="false" within RecyclerView-xml.
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="#dimen/height_toolbar"
android:paddingBottom="#dimen/height_bottom_bar_container"
//android:clipToPadding="false" <-- remove this flag!
android:scrollbars="vertical"
/>
paddingTopand paddingBottom worked, but we replaced it with some GapViews(empty views, with height of paddingTop and paddingBottom)
if someone is using | recyclerViewObject.setHasFixedSize(true);
try removing it, It was causing the issue in my case.
You can try setting your Viewgroup holding the recyclerview to wrap content
i have this problem and i just use
android:layout_height="wrap_content"
for parent of every item.
I need your help!
I have a main activity that contains 2 fragments (one for the header and one for the multicolumn list), one of the fragments is a ListFragments contains a multicolumn ListView, which is boxed in a scrollable view. This ListFragment overrides the onItemClickListener method. I can scroll the view and everything, but when I click on the listview, the listener is not invoked!
I have tried many different solutions, like trying to block focusability for views, but nothing has worked so far. Maybe you guys can help me out.
The parent activity containing the two fragments:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="1"
android:descendantFocusability="blocksDescendants"
tools:context=".MainWindow"
>
<fragment
android:id="#+id/headerfragment"
android:name="lindcom.mobile.estock.fragments.ListHeaderFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:descendantFocusability="blocksDescendants"
/>
<fragment
android:id="#+id/unitlistfragment"
android:name="lindcom.mobile.estock.fragments.UnitsListFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:descendantFocusability="blocksDescendants"
/>
</LinearLayout>
This is the layout I use for my multicolumn list:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="false"
android:focusableInTouchMode="false"
android:descendantFocusability="blocksDescendants" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="1" >
<TextView
android:id="#+id/unitsCol1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.40"
android:ellipsize="marquee"
android:maxLines="1"
android:scrollHorizontally="true"
android:singleLine="true"
/>
<TextView
android:id="#+id/unitsCol2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.40"
android:ellipsize="marquee"
android:maxLines="1"
android:scrollHorizontally="true"
android:singleLine="true"
/>
<TextView
android:id="#+id/unitsCol3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.2"
android:ellipsize="marquee"
android:maxLines="1"
android:scrollHorizontally="true"
android:singleLine="true"
/>
</LinearLayout>
</ScrollView>
The above layout is populated in an async task, after I have fetched the data:
ListAdapter adapter = new SimpleAdapter(
fragment.getActivity().getBaseContext(), this.unitList,
R.layout.unit_list_item, new String[] { TAG_OWNER, TAG_NAME,
TAG_CONTENT }, new int[] { R.id.unitsCol1, R.id.unitsCol2,
R.id.unitsCol3 });
fragment.setListAdapter(adapter); // Set the adapter to the listFragment
Then for the on click listener, I simply override it in the fragment:
public class UnitsListFragment extends ListFragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Fetch stuff here
}
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
String type = l.getItemAtPosition(position).getClass().toString();
Log.d("Item type was", type);
// THIS IS NEVER INVOKED :(
};
}
I have tried so many different things, but maybe you guys can see a simple fix to this, something that I might have overlooked.
Any help would be greatly appreciated! Thanks!
Remove your scollview in layout or define it as not focusable then try again
I have a view that contains a YouTubePlayerSupportFragment, with a small box around it for styling. I want to add multiple instances of this view on top of each other (vertically) to another view. I have it working for adding a single view, but when I try to add multiple they just stack on each other, so that only the one that was added last is visible. I was able to prove this was happening, by programatically making the first view added longer and I could see the extra height of it beneath the topmost view.
Here is my code:
fragment_youtube_video:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="5dp"
android:layout_marginTop="5dp"
android:background="#FFFFFFFF">
<fragment
android:id="#+id/youTubeVideo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:name="com.google.android.youtube.player.YouTubePlayerSupportFragment" />
<TextView
android:id="#+id/videoTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="4dp"
android:layout_marginBottom="8dp"
android:textColor="#000000"
android:text="Video" />
</LinearLayout>
fragment_videos:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/background">
<ImageView
android:id="#+id/topBar"
android:background="#000000"
android:layout_width="match_parent"
android:layout_height="45dp" />
<ImageView
android:id="#+id/logoImageView"
android:layout_width="120dp"
android:layout_height="60dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:contentDescription="#string/CD_logo"
android:src="#drawable/logo" />
<RelativeLayout
android:id="#+id/pagerTitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/logoImageView"
android:background="#80000000"
android:paddingBottom="10dp"
android:paddingLeft="2dp"
android:paddingRight="2dp"
android:paddingTop="10dp"
android:layout_marginLeft="12dp"
android:layout_marginTop="5dp"
android:layout_marginRight="12dp" >
[REMOVED FOR SPACE]
</RelativeLayout>
<ScrollView
android:id="#+id/scrollableVideoBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/pagerTitle"
android:layout_alignRight="#+id/pagerTitle"
android:layout_below="#+id/pagerTitle">
<LinearLayout
android:id="#+id/videosLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#a0000000">
</LinearLayout>
</ScrollView>
</RelativeLayout>
VideosFragment.java:
private void addVideoViews() {
int count = 0;
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
for (String vidURL : videoURLs) {
new YoutubeFragment();
YoutubeFragment player = YoutubeFragment.newInstance(count, vidURL);
fragmentTransaction.add(R.id.videosLayout, player, "x_" + count);
count++;
}
fragmentTransaction.commit();
}
I could provide more code, but I believe these are the necessary pieces. Please also feel free to let me know if I am going about this incorrectly. I am so close, I just can't figure out for the life of me why they are stacking.
Any help would be appreciated!
We went into Chat, and this is what we came to:
LinearLayout parentLayout = (LinearLayout) view.findViewById(R.id.videosLayout);
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
LayoutInflater inflater = LayoutInflater.from(getActivity());
for (String vidURL : videoURLs) {
final String vidUrl2 = vidURL;
LinearLayout placeholder = (LinearLayout) inflater.inflate(R.layout.fragment_youtube_video, container, false);
View youtuber = placeholder.findViewById(R.id.youTubeVideo);
YouTubePlayerSupportFragment player = new YouTubePlayerSupportFragment();
fragmentTransaction.add(youtuber.getId(), player);
placeholder.setId(12345);
parentLayout.addView(placeholder);
player.initialize("secret", new OnInitializedListener() {
#Override
public void onInitializationSuccess(Provider arg0, YouTubePlayer arg1,
boolean arg2) {
if (!arg2) {
arg1.cueVideo(vidUrl2);
}
}
#Override
public void onInitializationFailure(Provider arg0,
YouTubeInitializationResult arg1) {
}
});
}
fragmentTransaction.commit();
Placeholder is a LinearLayout with two children, a LinearLayout to hold the youtube fragment, and a TextView to hold the title.
I'm assuming, that when you add multiple videos, you're just swapping them within your fragment. There aren't multiple fragments, so only one video would ever show at any given time.
I think you should use a ListView instead, that way, you can populate it with numerous videos that all have the same general layout resource. Hopefully this helps.
I have a very basic ListView in android and had set a very basic adapter. My problem is that the list view does not show anything, regardless of the adapter and the notifyDataSetChanged();
Here is my code:
XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView android:text="#string/app_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
></TextView>
<ListView android:id="#+id/selectView"
android:layout_height="wrap_content"
android:layout_width="wrap_content">
</ListView>
</RelativeLayout>
The Activity code:
import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;
import com.androidcourse.phonemapper.R;
import com.androidcourse.phonemapper.model.SelectViewAdapter;
public class SelectActivity extends Activity {
private ListView mListView;
private SelectViewAdapter mAdapter;
#Override
public void onCreate(Bundle savedState) {
super.onCreate(savedState);
setContentView(R.layout.select_activity);
initializeListView();
}
private void initializeListView() {
mListView = (ListView) findViewById(R.id.selectView);
mAdapter = new SelectViewAdapter(this);
mListView.setAdapter(mAdapter);
mAdapter.notifyDataSetChanged();
}
#Override
public void onResume() {
super.onResume();
}
}
And the Adapter code:
import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
public class SelectViewAdapter extends BaseAdapter {
private Context mContext;
private TextView mMockTextView;
public SelectViewAdapter(Context cnt) {
mContext = cnt;
mMockTextView = new TextView(mContext);
mMockTextView.setText("Test text");
mMockTextView.setBackgroundColor(Color.CYAN);
}
#Override
public int getCount() {
return 3;
}
#Override
public Object getItem(int position) {
return mMockTextView;
}
#Override
public long getItemId(int position) {
return 3;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
return mMockTextView;
}
}
The problem is that nothing is shown on the screen. A black screen (and the first text view from the XML) is all I get. I cannot see the mockTextView and its text. Apparently I am doing something quite wrong, but I cant figure out what.
A few things I can think of.
First, Your RelativeLayout has no relative positioning information. I would assume you meant to put this in a LinearLayout with orientation set to vertical from what you describe. My guess is that the list is not actually being drawn since it isn't even anchored to anything in the current RelativeLayout. If you stick with the RelativeLayout, make sure to put an id on the app_name TextView and position the ListView under it via layout_below.
LinearLayout Solution
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView android:text="#string/app_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
></TextView>
<ListView android:id="#+id/selectView"
android:layout_height="wrap_content"
android:layout_width="wrap_content">
</ListView>
</LinearLayout>
RelativeLayout Solution:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="#+id/app_name_text"
android:text="#string/app_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
></TextView>
<ListView android:id="#+id/selectView"
android:layout_below="#id/app_name_text"
android:layout_height="wrap_content"
android:layout_width="wrap_content">
</ListView>
</RelativeLayout>
Next, your getView() returns the same textView for all 3 indexes. It's not a problem to display the same view over multiple indexes however with a list size of three, I am betting that the screen can display all three at the same time. And since a View can't be in more than one position at a time, I actually would expect this to fail so I doubt it is even getting to this code yet. Try creating a new TextView for each getView(). Also your MockTextView doesn't have layout params of it's own. So laying it out within a listView cell might not be happening either. So you can give it params of type AbsListView.LayoutParams(WRAP_CONTENT, WRAP_CONTENT). Again though I would expect this to error if it got to the original code.
getView() tidy up:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null) {
TextView textView = new TextView(parent.getContext());
textView.setText("Position is:"+position);
textView.setBackgroundColor(Color.CYAN);
textView.setLayoutParams(new AbsListView.LayoutParams(WRAP_CONTENT, WRAP_CONTENT);
convertView = textView;
}
return mMockTextView;
}
And lastly the wrap_content height of your list can sometimes be problematic. I am not aware of all the scenarios. If you end up changing to a LinearLayout try setting your layout_height of the list view to 0 and then set the layout_weight=1. This forces the linear layout to inflate it into more space.
LinearLayout Weight Solution:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView android:text="#string/app_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
></TextView>
<ListView android:id="#+id/selectView"
android:layout_weight="1"
android:layout_height="0dp"
android:layout_width="wrap_content">
</ListView>
</LinearLayout>
I was using the Data Binding example from the book Hello Android by Ed Burnette (great book).
I changed the item layout from A RelativeLayout to a LinearLayout; however, I did not add an orientation when I made the change.
Once I added android:orientation="vertical" everything worked fine.
Two hours of my life on this one.
While it does not answer this question, a typical issue is to inflate as adapter resource a layout based on a parent LinearLayout with height=wrap_content and each LinearLayout element with height=fill_parent.
Like in
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/parentLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/someText"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"/>
While it is displayed nicely in Eclipse, each row ends with height=0. This can be fixed by setting height=wrap_content for one of the child element
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/parentLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/someText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"/>
Could be helpfull for someone.
Make sure your adapter function getCount() is implemented and returns higher value then 0. If method returns 0, list is not shown or filled.
i had the same problem but when i changed the
ConstraintLayout
into
Relative layout
it become visible and problem solved.