2017-04-17 5 views
0

テキストと緑色の四角を含むListviewでアクティビティが発生しました。私は最終的に私の心を変え、アクティビティではなくフラグメントを使用するようにコードを修正しました。しかし、私のテキストの色が変わったので、もう緑色の四角は見えません。リストのコードやテキストの色を変更していないので、何が起こっているのか分かりません。アクティビティからフラグメントに切り替えた後のUIの外観が変更されました。

ありがとうございました。

enter image description here

MainActivityのみ断片容器として使用されている:

public class MainActivity extends AppCompatActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     FragmentManager fm = getSupportFragmentManager(); 

     Fragment fragment = fm.findFragmentById(R.id.ll_container); 
     if (fragment == null) { 
      fragment = new PollutionLevelsFragment(); 
      fm.beginTransaction() 
        .add(R.id.ll_container, fragment) 
        .commit(); 
     } 
    } 
} 

MainActivityレイアウト

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/ll_container" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 

断片クラス

public class PollutionLevelsFragment extends Fragment implements PollutionLevelsFragmentMVP.View { 
    private PollutionLevelAdapter plAdapter; 

    @BindString(R.string.city) 
    String city; 

    @BindString(R.string.aqicn_token) 
    String authToken; 


    @BindView(R.id.lv_pollution_levels) 
    ListView lvPollutionLevels; 

    @Inject 
    PollutionLevelsFragmentMVP.Presenter presenter; 

    private ViewModel pollutionData; 
    private ArrayList<String> testbidon; 

    @Override 
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) { 
     View view = inflater.inflate(R.layout.fragment_pollution_levels, container, false); 

     ButterKnife.bind(this,view); 

     // Construct the data source 
     ArrayList<PollutionLevel> arrayOfPollutionLevel = new ArrayList<>(); 
     // Create the adapter to convert the array to views 
     plAdapter = new PollutionLevelAdapter(getActivity().getApplicationContext(), arrayOfPollutionLevel); 

     lvPollutionLevels.setAdapter(plAdapter); 
     return view; 

    } 

    @Override 
    public void onStart() { 
     super.onStart(); 

     presenter.setView(this); 
     presenter.loadData(city, authToken); 
    } 

    @Override 
    public void onStop() { 
     super.onStop(); 
     presenter.rxUnsubscribe(); 
    } 

    @Override 
    public void onActivityCreated(@Nullable Bundle savedInstanceState) { 
     super.onActivityCreated(savedInstanceState); 

     ((App) getActivity().getApplication()).getComponent().inject(this); 
    } 

    @Override 
    public void updateData(ViewModel viewModel) { 
     this.pollutionData = viewModel; 

     ArrayList<PollutionLevel> pollutionLevels = viewModel.getAllPolluants(); 

     for(PollutionLevel pl : pollutionLevels) { 
      plAdapter.add(pl); 
     } 
    } 
} 

リストビュー

public class PollutionLevelAdapter extends ArrayAdapter<PollutionLevel> { 

@BindView(R.id.tv_name) 
TextView tvName; 

@BindView(R.id.tv_value) 
TextView tvValue; 

public PollutionLevelAdapter(Context context, ArrayList<PollutionLevel> pollutionLevels) { 
    super(context,0,pollutionLevels); 
} 


@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    if(convertView == null) { 
     convertView = LayoutInflater.from(getContext()).inflate(R.layout.list_item_pollution, parent, false); 
    } 

    View view = convertView; 

    ButterKnife.bind(this,view); 
    PollutionLevel pollutionLevel = getItem(position); 
    tvName.setText(pollutionLevel.getName()); 
    tvValue.setText(pollutionLevel.getValue()); 


    return view; 
} 
} 

マイリスト項目

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      xmlns:app="http://schemas.android.com/apk/res-auto" 
      android:orientation="horizontal" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent"> 

<ImageView 
    android:id="@+id/iv_warning" 
    android:layout_width="20dp" 
    android:layout_height="20dp" 
    app:srcCompat="@drawable/rectangle"/> 

<TextView 
    android:id="@+id/tv_name" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_margin="5dp" 
    android:gravity="center_vertical" 
    android:text="TextView"/> 

<TextView 
    android:id="@+id/tv_value" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_margin="5dp" 
    android:gravity="center_vertical" 
    android:text="TextView"/> 

ためこれは、レンダリングレイアウトアクティビティクラスのコードであるフラグメントレイアウト

<FrameLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <ListView android:id="@+id/lv_pollution_levels" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"/> 
</FrameLayout> 

ArrayAdapterリストビューを正しく(私は断片に対する活性)。それは変化しなかったので、私はここにリストビューアダプタのコードを入れていない:

活動

public class PollutionLevelsActivity extends AppCompatActivity implements PollutionLevelsActivityMVP.View { 
    private PollutionLevelAdapter plAdapter; 

    @BindString(R.string.city) 
    String city; 

    @BindString(R.string.aqicn_token) 
    String authToken; 


    @BindView(R.id.lv_pollution_levels) 
    ListView lvPollutionLevels; 

    @Inject 
    PollutionLevelsActivityMVP.Presenter presenter; 

    private ViewModel pollutionData; 
    private ArrayList<String> testbidon; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     ((App) getApplication()).getComponent().inject(this); 
     ButterKnife.bind(this); 

    // Construct the data source 
    ArrayList<PollutionLevel> arrayOfPollutionLevel = new ArrayList<>(); 
    // Create the adapter to convert the array to views 
    plAdapter = new PollutionLevelAdapter(this, arrayOfPollutionLevel); 

    lvPollutionLevels.setAdapter(plAdapter); 

    } 

    @Override 
    protected void onStart() { 
     super.onStart(); 

     presenter.setView(this); 
     presenter.loadData(city, authToken); 
    } 

    @Override 
    protected void onDestroy() { 
     super.onDestroy(); 
     presenter.rxUnsubscribe(); 
    } 

    @Override 
    public void updateData(ViewModel viewModel) { 
     this.pollutionData = viewModel; 

     ArrayList<PollutionLevel> pollutionLevels = viewModel.getAllPolluants(); 

     for(PollutionLevel pl : pollutionLevels) { 
      plAdapter.add(pl); 
     } 
    } 
} 

レイアウト

<android.support.constraint.ConstraintLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="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="com.livos.citypollution.pollutionlevels.PollutionLevelsActivity"> 

    <ListView 
     android:id="@+id/lv_pollution_levels" 
     android:layout_width="368dp" 
     android:layout_height="495dp" 
     tools:layout_editor_absoluteX="8dp" 
     tools:layout_editor_absoluteY="8dp" 
     app:layout_constraintBottom_toBottomOf="parent" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintRight_toRightOf="parent" 
     app:layout_constraintTop_toTopOf="parent"/> 

</android.support.constraint.ConstraintLayout> 
+0

私はちょうどこのaを変更することによって現れなかった四角形の問題を解決しましたp:srcCompat = "@ drawable/rectangle" 'をアンドroid:src =" @ drawable/rectangle "'に設定します。私はまだテキストの色に問題があります。 – Laurent

答えて

0

をテキストの色がこの

を追加変更するには

android:textColor = "あなたのカラーコード"

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      xmlns:app="http://schemas.android.com/apk/res-auto" 
      android:orientation="horizontal" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent"> 

<ImageView 
    android:id="@+id/iv_warning" 
    android:layout_width="20dp" 
    android:layout_height="20dp" 
    **app:src="@drawable/rectangle"**/> 

<TextView 
    android:id="@+id/tv_name" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_margin="5dp" 
    android:gravity="center_vertical" 
    android:text="TextView" 
    android:textColor="Your Color Code"/> 

<TextView 
    android:id="@+id/tv_value" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_margin="5dp" 
    android:gravity="center_vertical" 
    android:text="TextView" 
    android:textColor="Your Color Code"/> 
+0

はい、しかし、通常は、特定の色を指定しないと黒色のデフォルトの色があります。しかし、私は今それを得ると思う、私はそれが断片に当てはまらないが、活動に適用されるテーマと関係があると思う。または別のテーマが適用されます... – Laurent

+0

はい、しかし私の断片にactiviyのテーマの使用を適用する方法があるかどうか知っていますか? (実際にテーマの問題がある場合) – Laurent

+1

そして、マニフェストで定義されたテーマは、断片化ではないアクティビティにのみ適用されると思います。私はここに何かを見つけたと思うhttp://stackoverflow.com/questions/9469174/set-theme-for-a-fragment – Laurent

0

私はそうしています。

@BindView(R.id.iv_warning) 
ImageView iv_warning; 

とGetViewメソッドのセットで:あなたはあなたのImageViewのは次のように結合しそうImageViewを持っているあなたのlist_item_pollutionを与えたとして、あなたはあなたの2 TextViewないあなたImageViewを結合している

public class PollutionLevelAdapter extends ArrayAdapter<PollutionLevel> { 

@BindView(R.id.tv_name) 
TextView tvName; 

@BindView(R.id.tv_value) 
TextView tvValue; 

public PollutionLevelAdapter(Context context, ArrayList<PollutionLevel> pollutionLevels) { 
    super(context,0,pollutionLevels); 
} 


@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    if(convertView == null) { 
     convertView = LayoutInflater.from(getContext()).inflate(R.layout.list_item_pollution, parent, false); 
    } 

    View view = convertView; 

    ButterKnife.bind(this,view); 
    PollutionLevel pollutionLevel = getItem(position); 
    tvName.setText(pollutionLevel.getName()); 
    tvValue.setText(pollutionLevel.getValue()); 


    return view; 
} 
} 

:あなたは次のようにアダプタを与えます次のようになります:

iv_warning.setBackgroundColor(Color.parseColor("your color code")); 
+0

ButterKnifeを知っているように@BindViewはfindViewById()の定型句を避けるために使われています。私の正方形の色を変更する必要があります私のImageViewのための@ BindViewを使用する必要はありません正方形とそれの色は、私のリストアイテムレイアウトを使用するPollutionLevelAdapterを通じて定義されています。このリストアイテムレイアウトには、drawable srcが定義されたmy imageviewが含まれています。色はこのdrawable srcで定義されています。ちなみに私のコメントに入れてみましたが、これは 'android:src =" @ drawable/rectangle "'を使って解決しました。 – Laurent

+0

テキストの色には通常、デフォルトのテーマ色があります。色が指定されていない場合は黒です。もう少し研究を重ねた後、私の考えている問題は、その断片には当てはまらない活動の中にデフォルトのテーマがあるのですが、私はその理由を知らないということです。 – Laurent

関連する問題