2015-12-30 72 views
2

在我的活动中,我有一个FrameLayout,它假设用作碎片的容器。ScrollView里面的片段不滚动

在我的片段中,我得到了一个不响应的ScrollViewScrollView包含TextView集合android:layout_height="wrap_content",与我的java代码中的文本明显比ScrollView的大小更长。

我的片段XML:

<?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="match_parent" 
    android:orientation="vertical"> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 

     <ImageView 
      android:layout_width="@dimen/bookview.imageWidth" 
      android:layout_height="250dp" 
      android:id="@+id/bookView.image"/> 
     <TextView 
      android:textSize="@dimen/custom_book_text" 
      android:layout_height="wrap_content" 
      android:layout_width="fill_parent" 
      android:layout_toEndOf="@id/bookView.image" 
      android:id="@+id/bookView.name"/> 
     <TextView 
      android:textSize="@dimen/custom_book_text" 
      android:layout_height="wrap_content" 
      android:layout_width="fill_parent" 
      android:layout_toEndOf="@id/bookView.image" 
      android:layout_below="@id/bookView.name" 
      android:id="@+id/bookView.author"/> 
     <TextView 
      android:textSize="@dimen/custom_book_text" 
      android:layout_height="wrap_content" 
      android:layout_width="fill_parent" 
      android:layout_toEndOf="@id/bookView.image" 
      android:layout_below="@id/bookView.author" 
      android:id="@+id/bookView.pages"/> 
     <TextView 
      android:textSize="@dimen/custom_book_text" 

      android:layout_height="wrap_content" 
      android:layout_width="fill_parent" 
      android:layout_toEndOf="@id/bookView.image" 
      android:layout_below="@id/bookView.pages" 
      android:id="@+id/bookView.category"/> 
     <TextView 
      android:textSize="@dimen/custom_book_text" 
      android:layout_height="wrap_content" 
      android:layout_width="fill_parent" 
      android:layout_toEndOf="@id/bookView.image" 
      android:layout_below="@id/bookView.category" 
      android:id="@+id/bookView.publishingDate"/> 
    </RelativeLayout> 
    <ScrollView 
     android:layout_height="200dp" 
     android:layout_width="match_parent" 
     android:layout_gravity="end"> 
     <TextView 
      android:layout_height="wrap_content" 
      android:layout_width="match_parent" 
      android:textSize="@dimen/custom_book_text" 
      android:id="@+id/bookView.description"/> 
    </ScrollView> 
</LinearLayout> 

活动的XML:

<android.support.v4.widget.DrawerLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:id="@+id/use.drawer"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical"> 

     <FrameLayout 
      android:layout_width="match_parent" 
      android:layout_height="@dimen/user_custom_book_height" 
      android:id="@+id/user.customBookContainer"/> 
     <FrameLayout 
      android:layout_width="match_parent" 
      android:layout_height="fill_parent" 
      android:id="@+id/user.container"/> 
    </LinearLayout> 

    <include 
     layout="@layout/navigation_drawer" 
     android:layout_width="240dp" 
     android:layout_height="match_parent" 
     android:layout_gravity="start"/> 
</android.support.v4.widget.DrawerLayout> 

我使用的FrameLayout,因为我想改变该区域中的片段,在runtime.better想法?我喜欢听到。

我已经尝试减少ScrollView的高度。 我的片段java代码:

public class BookView extends Fragment { 

    private Bitmap bitmap; 
    private String name; 

    public static BookView newInstance(String book,Bitmap bitmap) { 

     Bundle args = new Bundle(); 
     BookView fragment = new BookView(); 
     fragment.bitmap = bitmap; 
     fragment.name = book; 
     fragment.setArguments(args); 
     return fragment; 
    } 

    public BookView() { 
     // Required empty public constructor 
    } 


    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     // Inflate the layout for this fragment 
     View v = inflater.inflate(R.layout.fragment_book_view, container, true); 
     Backend backend = BackendFactory.getInstance(); 
     try { 
      Book b = backend.readBook(name); 
      TextView textView = (TextView)v.findViewById(R.id.bookView_name); 
      textView.setText(b.getBookName()); 
      textView = (TextView)v.findViewById(R.id.bookView_author); 
      textView.setText(b.getAuthor()); 
      textView = (TextView)v.findViewById(R.id.bookView_category); 
      textView.setText(b.getCategory().name()); 
      textView = (TextView)v.findViewById(R.id.bookView_description); 
      textView.setText(b.getDescription()); 
      textView = (TextView)v.findViewById(R.id.bookView_pages); 
      textView.setText(Integer.toString(b.getPages())); 
      textView = (TextView)v.findViewById(R.id.bookView_publishingDate); 
      textView.setText(b.getPublishingDate()); 
      final ImageView imageView = (ImageView)v.findViewById(R.id.bookView_image); 
      if(bitmap != null){ 
       imageView.setImageBitmap(bitmap); 
      } else{ 
       ImageLoader imageLoader = new ImageLoader(); 
       imageLoader.setListener(new ImageLoader.ImageTaskListener() { 
        @Override 
        public void onActionEnd() {} 

        @Override 
        public void onImageDownload(Bitmap bitmap) { 
         imageView.setImageBitmap(bitmap); 
        } 
       }); 
       imageLoader.execute(b.getImage()); 
      } 
     } 
     catch (Exception e) { 
      AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 
      builder.setMessage(e.getMessage()); 
      builder.create().show(); 
     } 

     return inflater.inflate(R.layout.fragment_book_view, container, false); 
    } 
} 

ImageLoader的是一类我写的,它推广的的AsyncTask下载图像从一个URL,当它准备更新UI。

+0

为什么你使用framelayout的片段,有什么特别的原因? –

+0

你可以发布你的代码,截图会更好吗? –

+1

尝试使用线性布局或相对而非framelayout。 –

回答

0

我假设你能够看到片段的视图。

如果是这样几件事情尝试:

设置滚动视图fillViewPort =“真” ,然后设置文本视图匹配父,因为这是在滚动视图的唯一视图。作为一个说明,你应该避免使用硬编码宽度&高度(甚至是dp高度)。

,而不是返回

return inflater.inflate(R.layout.fragment_book_view, container, false); 

只是返回原来的观点:v易

诺特尔”可读性的一点:不是TextView的每一次重新分配。 做:

((TextView)v.findViewById(R.id.blabla)).setText("BLA"); 

编辑:

我已经测试过这个&它的工作原理。

<ScrollView 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_weight="2" 
    android:fillViewport="true"> 
    <TextView 
     android:id="@+id/faq_output_answer" 
     android:layout_width="match_parent" 
     android:layout_margin="10dp" 
     android:layout_height="match_parent" /> 
</ScrollView> 
+0

它不起作用。 – user4349397

+0

你可以看到textview中的文本(即滚动视图内)?也尝试编辑。 – Austi01101110

+0

我检查了你的代码,仍然是同样的反应,当我触摸ScrollView时,我看到灰色的滚动条,但我不滚动。因此,我告诉他,它触发了事件,并且它应该滚动,因为灰色滚动条更小,当我无法看到全文。 – user4349397

0

我觉得这个片段的其他部分(TextViews和ImageViews)充满了屏幕的所有区域,并且ScrollView渲染出了屏幕。 我建议你把所有的布局放在ScrollView里面。 这样的:

<?xml version="1.0" encoding="utf-8"?> 
<ScrollView 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_gravity="end"> 

<LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical"> 

    <RelativeLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content"> 

     <ImageView 
       android:id="@+id/bookView.image" 
       android:layout_width="@dimen/bookview.imageWidth" 
       android:layout_height="250dp"/> 

     <TextView 
       android:id="@+id/bookView.name" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_toEndOf="@id/bookView.image" 
       android:textSize="@dimen/custom_book_text"/> 

     <TextView 
       android:id="@+id/bookView.author" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_below="@id/bookView.name" 
       android:layout_toEndOf="@id/bookView.image" 
       android:textSize="@dimen/custom_book_text"/> 

     <TextView 
       android:id="@+id/bookView.pages" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_below="@id/bookView.author" 
       android:layout_toEndOf="@id/bookView.image" 
       android:textSize="@dimen/custom_book_text"/> 

     <TextView 
       android:id="@+id/bookView.category" 

       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_below="@id/bookView.pages" 
       android:layout_toEndOf="@id/bookView.image" 
       android:textSize="@dimen/custom_book_text"/> 

     <TextView 
       android:id="@+id/bookView.publishingDate" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_below="@id/bookView.category" 
       android:layout_toEndOf="@id/bookView.image" 
       android:textSize="@dimen/custom_book_text"/> 
    </RelativeLayout> 

    <TextView 
      android:id="@+id/bookView.description" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:textSize="@dimen/custom_book_text"/> 

</LinearLayout> 

+0

我只想要说明TextView滚动所有其他东西必须是静态的 – user4349397

+0

其他部分的大小是全屏。在小屏幕上,你不能处理这个! – Fred

+0

这是在小屏幕上的一个大麻烦!您必须选择以下策略之一: - 使用两个ScrollView处理顶部和底部部分。这两个ScrollView填充所有屏幕区域。 - 使用我上面的建议 – Fred

0

尝试把一个空的观点在你的滚动视图中的最后一项。

<View 
android:layout_width = "match_parent" 
android:layout_height = "50dp" /> 

这对我有效。