2016-10-18 2 views
0

에 항목을 표시하는 RecyclerView이 있습니다. 항목에는 이미지보기가 포함되며 사용자가 항목을 선택하면 채워지는 TextView 이 포함됩니다. 그들이 항목을 선택하고 나면 선택한 항목의 배경을 검색하고 새로운 활동을 열 계획을 발표합니다.이 활동에서 레이아웃을 동적으로 만들고 선택한 배경의 배경을 레이아웃의 배경으로 설정합니다. 이것은 내 어댑터 클래스입니다 내 RecyclerView 내가 선택한 항목의 배경을 검색 할 수 있습니다 채 웁니다.작은 화면 장치에서 Recyclerview가 선택한 항목의 배경 이미지가 너무 큽니다.

public class PersonsAdapter extends RecyclerView.Adapter<PersonsAdapter.MyViewHolder> { 

     private List<Person> mpersonsList; 


     public class MyViewHolder extends RecyclerView.ViewHolder { 
      public TextView name .....; 
      View v; 
      Person p; 

      public MyViewHolder(View view) { 
       super(view); 
       name = (TextView) view.findViewById(R.id.tvName); 
       //and all other textviews.... 

       v = view; 
       v.setOnClickListener(new View.OnClickListener() { 
        @Override 
        public void onClick(View view) { 
         //if (view.getId() == getAdapterPosition()) { 
          //int itemposition = mRecyclerView.indexOfChild(v); 
          Intent i = new Intent(context, SelectedTemplate.class); 
          startActivity(i); 
          theBackground = view.getBackground();//retrieve the background of the selected item 
         //} 
        } 
       }); 
       List<Drawable> templatesList = new ArrayList<>(); 
      } 
     } 


     public PersonsAdapter(List<Person> personsList) { 
      this.mpersonsList = personsList; 
     } 

     @Override 
     public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
      View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.person, parent, false); 

      return new MyViewHolder(itemView); 
     } 

     @Override 
     public void onBindViewHolder(MyViewHolder holder, int position) { 
      Person person = mpersonsList.get(position); 
      holder.name.setText(person.getName()); 
      //and other details of the item...... 

      holder.p = mpersonsList.get(position); 

      if(position == 0){ 
       holder.v.setBackgroundResource(R.drawable.wavy_curves_onblack); 
      } 
     //and other background images..... 
     } 

     @Override 
     public int getItemCount() { 
      return mpersonsList.size(); 
     } 
    } 

다음 활동에서 나는 다음과 같이 선택한 항목을 검색합니다.

private void getSelectedTemplate() { 
     LayoutInflater vi = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     View v = vi.inflate(R.layout.selected_template, null); 

     // fill in any details dynamically here 


     //insert into main view 
     ViewGroup insertPoint = (ViewGroup) findViewById(R.id.selectedtemplatelayout); 
     insertPoint.setBackground(TheTemplates.theBackground); 
     insertPoint.addView(v, 0, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)); 

     rl = (LinearLayout) findViewById(R.id.selectedtemplatelayout); 
     image.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT)); 
     image.getLayoutParams().height = (int) getResources().getDimension(R.dimen.image_height); 
     image.getLayoutParams().width = (int) getResources().getDimension(R.dimen.image_width); 



     name.setText("NAME"); 
     name.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT)); 
     //add all other textviews dynamically..... 

     rl.removeAllViews(); 
     rl.addView(image); 
     rl.addView(name); 
     //add all remaining textviews 
    } 

내가 에뮬레이터 또는 미만 5.5inches있는 장치에서 테스트

내가 매우 큰 배경을 볼 (I 영원히 스크롤 할 수 있습니다) 그리고 난 단지 두 개의 이미지가 아닌 textviews을 볼 수 있습니다. 더 큰 장치 (실제 및 에뮬레이터 모두)에서 나는 적절한 크기의 배경을 얻습니다. 그러나 작은 기기처럼 텍스트보기가 표시되지 않습니다. 나는 두 이미지를 볼 수 있습니다.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:orientation="vertical" 
    android:focusable="true" 
    android:focusableInTouchMode="true"><!--this two lines will hide the keyboard and make the NAME textview visible --> 
    <android.support.v7.widget.Toolbar 
     android:id="@+id/my_toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:theme="@style/ThemeOverlay.AppCompat.ActionBar" 
     app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/> 
    <ScrollView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 

    <LinearLayout 
     android:id="@+id/selectedtemplatelayout" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:padding="4dp" 
     android:orientation="vertical" > 
//this is the layout I would like to stuff all my textviews and images and //set the background of the selected item. 
    </LinearLayout> 

    </ScrollView> 


//another layout 
    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:fillViewport="true"> 

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

내가 이런 일이 어떻게 내가 그것을 고칠 수 이유를 알아낼 수없는 RecyclerView

<?xml version="1.0" encoding="utf-8"?> 
<!--<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent">--> 

    <android.support.v7.widget.RecyclerView 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:app="http://schemas.android.com/apk/res-auto" 
     android:id="@+id/templateList" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_gravity="center_horizontal" 
     /> 
<!--</LinearLayout>--> 

입니다. 어떤 도움을 주셔서 감사합니다.

+0

복수 해상도를위한 화면 개발 연습 –

답변

0

단순히 300dp 또는 무언가 같이 제한된 크기에 wrap_content에서 레이아웃 높이를 변경하고 모든 화면 크기에 걸쳐 작동하는 것 같다하여 크기 문제를 해결했다. 그러나 텍스트보기는 이상한 모든 화면에서 볼 수 없습니다. 내가이 문제를 고칠 때이 대답을 업데이트하여 언젠가 누군가를 도울 수 있습니다.

+1

일반 화면에 320dp 사용 –

1

같은 폴더 생성

모바일 :

values      (For mdpi devices) 
values-hdpi    (For hdpi devices) 
values-xhdpi    (For xhdpi devices) 
values-xxhdpi    (For xxhdpi devices) 

정제 :

layout-sw600dp    (For 7? to 9? Screen) 
layout-sw720dp    (For 10? to above screen) 
: 아래 언급이다 다른 레이아웃 폴더를 만듭니다 아래 언급이다 다른 값 폴더를 만듭니다

이미지 리소스 용 : Creat 전자 4 개 당김 폴더 :

drawable-mdpi 
drawable-hdpi 
drawable-xhdpi 
drawable-xxhdpi 

그리고 당신은 API를 통해 처리하는 경우. 사용하는 서로 다른 이미지 크기 imagesmall_link imagelarge_link imagexlarge_link

+0

도움을 주셔서 감사합니다. 나는 앱을 계속 개발하면서 고려할 것입니다. –

+0

배경으로 사용하고있는 xml로 만든 도형을 가지고 있다면 어떻게 적용 할 수 있습니까? –

+0

귀하의 질문을 이해할 수 없으므로 구체적으로 기재하십시오. –