ScrollView inside ViewPager - android

Please Help me.
I searched everywhere how I can make a ScrollView inside a ViewPager but it was not successfully. The hole layout of the ViewPager should be scrollable.
Important: I only want a ScrollView in the vertical.
Already now I want to say thanks for the answers!
My code
"Main"
public class Rezepte2 extends Fragment {
// Declare Variables
ViewPager viewPager;
PagerAdapter adapter;
String[] rezeptTitel;
String[] naehrwerte;
String[] zubereitung;
int[] rezeptImage;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the view from viewpager_main.xml
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View layout = inflater.inflate(R.layout.rezepte_fragment, container, false);
// Generate sample data
rezeptTitel = new String[] { "Nudeln","Fleisch" };
naehrwerte = new String[] { "50kcal","70kcal" };
zubereitung = new String[] { "ganz easy","voll easy" };
rezeptImage = new int[] { R.drawable.ic_action_next_item, R.drawable.ic_action_next_item,
R.drawable.ic_launcher, R.drawable.ic_action_next_item };
// Locate the ViewPager in viewpager_main.xml
viewPager = (ViewPager) layout.findViewById(R.id.viewPager);
// Pass results to ViewPagerAdapter Class
adapter = new ViewPagerAdapter(getActivity(), rezeptTitel, naehrwerte, zubereitung, rezeptImage);
// Binds the Adapter to the ViewPager
viewPager.setAdapter(adapter);
return layout;
}
Adapter
public class ViewPagerAdapter extends PagerAdapter {
// Declare Variables
Context context;
String[] rezeptTitel;
String[] naehrwerte;
String[] zubereitung;
int[] rezeptImages;
LayoutInflater inflater;
public ViewPagerAdapter(Context context, String[] rezeptTitel, String[] naehrwerte,
String[] zubereitung, int[] rezeptImages) {
this.context = context;
this.rezeptTitel = rezeptTitel;
this.naehrwerte = naehrwerte;
this.zubereitung = zubereitung;
this.rezeptImages = rezeptImages;
}
#Override
public int getCount() {
return rezeptTitel.length;
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view == ((RelativeLayout) object);
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
// Declare Variables
TextView txtrezeptTitel;
TextView txtnaehrwerte;
TextView txtzubereitung;
ImageView imagerezept;
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = inflater.inflate(R.layout.rezepte_fragment_basic, container,
false);
// Locate the TextViews in viewpager_item.xml
txtrezeptTitel = (TextView) itemView.findViewById(R.id.rezeptTitel);
txtnaehrwerte = (TextView) itemView.findViewById(R.id.naehrwerte);
txtzubereitung = (TextView) itemView.findViewById(R.id.zubereitung);
// Capture position and set to the TextViews
txtrezeptTitel.setText(rezeptTitel[position]);
txtnaehrwerte.setText(naehrwerte[position]);
txtzubereitung.setText(zubereitung[position]);
// Locate the ImageView in viewpager_item.xml
imagerezept = (ImageView) itemView.findViewById(R.id.image_rezepte_basic);
// Capture position and set to the ImageView
imagerezept.setImageResource(rezeptImages[position]);
// Add viewpager_item.xml to ViewPager
((ViewPager) container).addView(itemView);
return itemView;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
// Remove viewpager_item.xml from ViewPager
((ViewPager) container).removeView((RelativeLayout) object);
}
}
SwipeView xml
`
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/viewPager"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</LinearLayout>`
ViewPager fragment xml
`
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/holo_red_light" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/rezeptTitel"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="4">
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="#drawable/ic_launcher"
android:id="#+id/image_rezepte_basic"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="#+id/naehrwerte" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="#+id/zubereitung" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>ยด

maybe you're just not scrolling correctly - an emulator doesn't really have a touch screen. You have to touch and hold part of it while panning with another finger

Related

How to create hover text on image slider android?

I have an image slider with the bottom code:
public class MainActivity extends Activity {
private class ImagePagerAdapter extends PagerAdapter {
private final int[] mImages = new int[] {
R.drawable.chiang_mai,
R.drawable.himeji,
R.drawable.petronas_twin_tower,
R.drawable.ulm
};
#Override
public void destroyItem(final ViewGroup container, final int position, final Object object) {
((ViewPager) container).removeView((ImageView) object);
}
#Override
public int getCount() {
return this.mImages.length;
}
#Override
public Object instantiateItem(final ViewGroup container, final int position) {
final Context context = MainActivity.this;
final ImageView imageView = new ImageView(context);
final int padding = context.getResources().getDimensionPixelSize(
R.dimen.padding_medium);
imageView.setPadding(padding, padding, padding, padding);
imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
imageView.setImageResource(this.mImages[position]);
((ViewPager) container).addView(imageView, 0);
return imageView;
}
#Override
public boolean isViewFromObject(final View view, final Object object) {
return view == ((ImageView) object);
}
}
#Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
final ImagePagerAdapter adapter = new ImagePagerAdapter();
viewPager.setAdapter(adapter);
final CirclePageIndicator circleIndicator = (CirclePageIndicator) findViewById(R.id.indicator);
circleIndicator.setViewPager(viewPager);
final TextView tvText = (TextView) findViewById(R.id.text);
tvText.setText(getResources().getString(R.string.bodyText));
}
}
And this is the XML code:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<android.support.v4.view.ViewPager
android:id="#+id/view_pager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
/>
<com.viewpagerindicator.CirclePageIndicator
android:id="#+id/indicator"
android:padding="10dip"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
/>
<TextView
android:id="#+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
So I need to set a text Hover on the Viewpager so that when I swipe it, it will show up for a specific picture. How can I create a hover text on this image slider while making it run automatically for some specific time, and after a swipe show a hover text.
Have you just one XML Layout? If you've two it makes it quite alot easier to work with.
So this layout below is where the actual image slider will be place on the Home Fragment in this case.
<RelativeLayout 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"
tools:context="com.example.****.****.HomeFragment"
android:background="#000000"
>
<android.support.v4.view.ViewPager
android:id="#+id/view_pager"
android:layout_width="fill_parent"
android:layout_height="204dp"
android:scaleType="centerCrop"
></android.support.v4.view.ViewPager>
Secondly, this XML Layout below is the Layout for the ViewPager itself. As you can see it is made up of an ImageView and a TextView. With the TextView placed on top of the ImageView.
Hope this helps a bit.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/image_view"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:scaleType="centerCrop"
/>
<TextView
android:id="#+id/myImageViewText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/image_view"
android:layout_alignTop="#+id/image_view"
android:layout_alignRight="#+id/image_view"
android:layout_alignBottom="#+id/image_view"
android:layout_margin="1dp"
android:gravity="bottom"
android:textSize="35dp"
android:text="******"
android:textStyle="bold"
android:textColor="#17baef" />
</RelativeLayout>

How to enable full page swiping in viewpager?

How can I make swipe available in the middle of the screen on the VIewPager? Like on the app store, you can swipe anywhere to go to the next tab! Currently that happens only when I swipe from the extreme corner of the screen(Bezel Swipe)
MY code:
public class SwipeFragment extends Fragment {
ViewPager viewPager;
SlidingTabLayout slidingTabLayout;
ArrayList<String> tabs;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.swipe_fragment, container, false);
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
tabs = new ArrayList<String>();
tabs.add("Image");
tabs.add("Something Else");
tabs.add("Some Long Title");
tabs.add("Songs");
tabs.add("Off The Page!");
viewPager = (ViewPager) view.findViewById(R.id.viewPager);
viewPager.setAdapter(new VPAdapterHelper());
slidingTabLayout = (SlidingTabLayout) view.findViewById(R.id.slidingTabs);
slidingTabLayout.setViewPager(viewPager);
}
public class VPAdapterHelper extends PagerAdapter {
#Override
public int getCount() {
return tabs.size();
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
View view = getActivity().getLayoutInflater().inflate(R.layout.pager_item, container, false);
container.addView(view);
ListView listView = (ListView) view.findViewById(R.id.VP_ListView);
// TODO set different adapters for different tabs
listView.setAdapter(new AdapterHelper(context));
return view;
}
#Override
public boolean isViewFromObject(View view, Object obj) {
return obj == view;
}
#Override
public CharSequence getPageTitle(int position) {
return tabs.get(position);
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
}
swipe_fragment.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.nischal.test.SlidingTabLayout
android:id="#+id/slidingTabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<android.support.v4.view.ViewPager
android:id="#+id/viewPager"
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight="1" />
</LinearLayout>
pager_item.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"
android:orientation="vertical">
<ListView
android:id="#+id/VP_ListView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
I want to be able to swipe to different tabs from anywhere on the page.
I tried overriding the DragListner of the container and dispatched a draylistner to the viewpage, but did not help!
Any help will be appreciated!
Found the answer to my own question. The problem seems to be in the listview item layout.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="1dip"
android:paddingBottom="1dip"
android:paddingStart="8dip"
android:paddingEnd="8dip">
<ImageView
android:id="#+id/CL_imageView"
android:layout_width="48dp"
android:layout_height="48dp" />
<TextView
android:id="#+id/CL_textView"
android:layout_width="match_parent"
android:layout_height="48dp"
android:ellipsize="end"
android:gravity="center"
android:lineSpacingMultiplier="1.2"
android:singleLine="true"
android:text="New Text"
android:textSize="16sp"
android:textStyle="bold" />
</LinearLayout>
here, the textview gravity tag was set to center, which for some reason caused the whole problem. removed the gravity tag and problem solved!

Using viewpager for scrolling children inside a view

I have a menu which looks like this http://flashspeaksactionscript.com/wp-content/uploads/2008/06/vert-menu18.jpg Considering the child as that "3" in the picture I would like to be able to swipe that "3" to the left and see be able to see another item.
Here's my xml code:
<LinearLayout
android:id="#+id/llayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="45dp" >
<Button
android:id="#+id/Sum"
style="#style/SummaryButtons"
android:layout_width="100dp"
android:layout_height="100dp"
android:text="Item 1" />
<Button
android:id="#+id/nana"
style="#style/SummaryButtons"
android:layout_width="100dp"
android:layout_height="100dp"
android:text="Item 1" />
<Button
android:id="#+id/mama"
style="#style/SummaryButtons"
android:layout_width="100dp"
android:layout_height="100dp"
android:text="Item 1" />
</LinearLayout>
<LinearLayout
android:id="#+id/example_get_by_id"
android:layout_width
etc. These are the children. For example I want to be able to swipe through these buttons, just because I cannot use HorizontalScrollViewbecause my application is consisted of fragments and already has a swipe ability to surf through fragments. The swipe ability for those fragments and HorizontalScrollView get mixed up so, that's why I need to be able to use Viewpager for single items and not the whole page. Is there any at least an example for this? I couldn't find anything.
Thanks in advance.
have a look this may be useful to you:
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ViewPagerAdapter adapter = new ViewPagerAdapter(this, imageArra);
ViewPager myPager = (ViewPager) findViewById(R.id.myfivepanelpager);
myPager.setAdapter(adapter);
myPager.setCurrentItem(0);
}
private int imageArra[] = { R.drawable.gallery_photo_1, R.drawable.gallery_photo_2,
R.drawable.gallery_photo_3, R.drawable.gallery_photo_5,
R.drawable.gallery_photo_6, R.drawable.gallery_photo_7,
R.drawable.gallery_photo_8, R.drawable.ic_launcher };
}
and ViewPagerAdapter:
public class ViewPagerAdapter extends PagerAdapter {
Activity activity;
int imageArray[];
public ViewPagerAdapter(Activity act, int[] imgArra) {
imageArray = imgArra;
activity = act;
}
public int getCount() {
return imageArray.length;
}
public Object instantiateItem(View collection, int position) {
ImageView view = new ImageView(activity);
view.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT));
view.setScaleType(ScaleType.FIT_XY);
view.setBackgroundResource(imageArray[position]);
((ViewPager) collection).addView(view, 0);
return view;
}
#Override
public void destroyItem(View arg0, int arg1, Object arg2) {
((ViewPager) arg0).removeView((View) arg2);
}
#Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == ((View) arg1);
}
#Override
public Parcelable saveState() {
return null;
}
}
xml file:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<android.support.v4.view.ViewPager
android:id="#+id/myfivepanelpager"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="#+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" >
</ImageView>
</android.support.v4.view.ViewPager>
</LinearLayout>

android dynamic text with view pager

I have implemented a view pager in android app. I want dynamic text views to be displayed at the top and bottom of the image view. How to set different texts for every image on view pager? Let me know your suggestions.
Thanks.
You need to create custom PagerAdapter for this. I will show you a littel example :
Your Activity :
public class MainActivity
extends Activity{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.view_main);
List<CustomObject> items = new ArrayList<CustomObject>();
items.add(new CustomObject("First Top","First Bot"));
items.add(new CustomObject("Second Top","Second Bot"));
items.add(new CustomObject("Third Top","Third Bot"));
ViewPager viewPager = (ViewPager)findViewById(R.id.viewPager);
CustomPagerAdapter customPagerAdapter = new CustomPagerAdapter(this,items);
viewPager.setAdapter(customPagerAdapter);
}}
Your adapter :
public class CustomPagerAdapter
extends PagerAdapter{
List<CustomObject> items;
LayoutInflater inflater;
public CustomPagerAdapter(Context context, List<CustomObject> items)
{
this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.items = items;
}
#Override
public boolean isViewFromObject(View view, Object object)
{
return view == ((RelativeLayout) object);
}
#Override
public int getCount()
{
return items.size();
}
#Override
public void destroyItem(ViewGroup container, int position, Object object)
{
((ViewPager) container).removeView((View)object);
}
#Override
public Object instantiateItem(ViewGroup container, int position)
{
View itemView;
itemView = inflater.inflate(R.layout.your_item_layout, container, false);
TextView topTextItem = (TextView) itemView.findViewById(R.id.topText);
TextView bottomTextItem = (TextView) itemView.findViewById(R.id.bottomText);
CustomObject customObject = items.get(position);
topTextItem.setText(customObject.topText);
bottomTextItem.setText(customObject.bottomText);
((ViewPager) container).addView(itemView);
return itemView;
}}
Main layout - view_main :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="vertical" >
<android.support.v4.view.ViewPager
android:id="#+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
Layout item - your_item_layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/topText"
style="#android:style/TextAppearance.Medium"
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Top Text" />
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="#android:drawable/ic_delete" />
<TextView
style="#android:style/TextAppearance.Medium"
android:layout_alignParentBottom="true"
android:id="#+id/bottomText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:gravity="center"
android:text="Bottom Text" />
Best wishes.
you can do this in your Fragment class
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.spannerbord_image_new, null);
// RelativeLayout
// layout=(RelativeLayout)v.findViewById(R.id.spanner_image_relative);
Log.v("TEST", "is this printing everytime");
TextView tv = (TextView ) v.findViewById(R.id.my_tv );
}
i hope this works

Fragment overlaying an Activity layout

I'm having problems working with fragments. The problem is: I have a layout in my activity with a ListView. When i select an item in this ListView, instantiates a fragment replacing the activity layout. My result is both fragment and activity contents are showing on screen, overlaying each other. What can I fix that (the fragment just replacing the activity content)? Here is the codes:
activityLayout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/activity"
android:orientation="vertical">
<FrameLayout android:id="#+id/fragment_to_replace"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="visible"
android:orientation="vertical"
android:layout_gravity="center" >
<TextView android:id="#+id/textView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:visibility="visible"
android:text="#string/textView_select_table"
android:textSize="30dp"/>
<ListView android:layout_marginTop="20dp"
android:layout_width="fill_parent"
android:layout_height="200dp"
android:id="#+id/listView"
android:visibility="visible"
android:paddingTop="40dp" />
</FrameLayout>
</LinearLayout>
Activity.java
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity);
View view = findViewById(R.id.activity);
//Getting the ListView
ListView listView = (ListView) view.findViewById(R.id.listView);
listView.setTextFilterEnabled(true);
//Setting the adapter
listView.setAdapter(new Adapter(this));
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
//Creates a fragment
addFragment();
}
});
}
public void addFragment(){
MyFragment newFragment;
FragmentTransaction newFragmentTransaction = getSupportFragmentManager().beginTransaction();
newFragment = (MyFragment) getSupportFragmentManager()
.findFragmentById(R.id.fragment);
if (newFragment == null){
newFragment = new MyFragment(this, getSupportFragmentManager());
newFragmentTransaction.replace(R.id.fragment_to_replace,
new SelectDrinkFragment(this, getSupportFragmentManager()));
newFragmentTransaction.addToBackStack(null);
newFragmentTransaction.commit();
}
}
fragmentLayout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/fragment"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:visibility="visible"
android:text="#string/textView_two"
android:textSize="20dp"/>
<LinearLayout android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:visibility="visible"
android:text="#string/textView" />
<TextView android:id="#+id/textView_two"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:visibility="visible"/>
</LinearLayout>
<TextView
android:id="#+id/textView_three"
android:layout_marginTop="10dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:visibility="visible"
android:text="#string/textView_three"/>
<ListView
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:id="#+id/listView_two"
android:visibility="visible"
android:paddingTop="40dp" />
</LinearLayout>
Fragment.java
public View onCreateView(final LayoutInflater inflater, final ViewGroup container, Bundle saveInstanceState){
View view = inflater.inflate(R.layout.fragment, container, false);
final ListView listView = (ListView) view.findViewById(R.id.listView_two);
CharSequence [] list = getResources().getStringArray(R.array.array);
listView.setAdapter(new ListAdapter(getContext(), list));
return view;
}
ListAdapter.java
public class ListAdapter extends BaseAdapter{
private Context context;
private CharSequence[] drinkArraySize;
public ListAdapter(Context context, CharSequence[] array){
super();
this.context = context;
this.array= array;
}
#Override
public int getCount() {
return array.length;
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return 0;
}
public Context getContext(){
return context;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LinearLayout linearLayout = new LinearLayout(getContext());
RadioButton radioButton = new RadioButton(getContext());
radioButton.setText(array[position]);
linearLayout.addView(radioButton);
return linearLayout;
}
}
FragmentTransaction.replace() looks like it only replaces another fragment (and not just any View), so I'm pretty sure this is expected behavior (or at least, I know I've had this happen to me before). I suppose you have a few options:
I don't see in your code anything that would make the fragment's background opaque. You could try that first and see if it helps. If it does, that might be the simplest.
You could just set the other items' visibilities to View.GONE at the same time you perform your transaction, but that has some issues as far as backstack goes.
You could replace the ListView and TextView with a ListFragment instead. This is probably the "best" way to do it, but also is probably the most work.

Categories

Resources