Изменение макета RecyclerView при изменении ориентации работает только при первом просмотре

Spring не может найти компоненты, поэтому попробуйте аннотировать с @Repository

@Repository
public class CustomPostRepositoryImpl implements PostRepositoryCustom {

  @PersistenceContext
  private EntityManager em;

  @Override
  public void refresh(Post post) {
    em.refresh(post);       
  }
}

и

@Repository
public interface PostRepository extends CrudRepository<Post,Long>{

}
4
задан DolDurma 19 January 2019 в 15:37
поделиться

3 ответа

Это может звучать странно, но единственное правильное решение для этого случая - поместить эти настройки в ресурсы. ОС Android собирает ресурсы на основе множества свойств, и одно из них имеет наименьшую ширину. Чтобы отделить список от сетки: values ​​/ bools.xml :

<resources>
    <bool name="is_grid">false</bool>
</resources>

другой будет values-sw520dp / bools.xml :

[ 111]

Для числа столбцов одинаковая логика values-sw520dp / inteers.xml :

<resources>
    <integer name="grid_column_count">2</integer>
</resources>

И values-sw720dp / inteers.xml : 118]

<resources>
    <integer name="grid_column_count">3</integer>
</resources>

Так что в коде это будет что-то вроде:

if (getResources().getBoolean(R.bool.is_grid)) {
    instagram_feeds.setLayoutManager(new GridLayoutManager(getActivity(), getResources().getInteger(R.integer.grid_column_count));
} else {
    instagram_feeds.setLayoutManager(new LinearLayoutManager(getActivity());
}

Это должно применяться всякий раз, когда вы создаете свое представление, не нужно обрабатывать изменение конфигурации - это уже сделано для вас. Дайте ОС сделать свою работу.

0
ответ дан Viktor Yakunin 19 January 2019 в 15:37
поделиться

Чтобы справиться с изменением ориентации и работать соответственно, я сделал следующее:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        Log.i("SCREEN DP","Orientation:"+ getResources().getConfiguration().orientation +" Width = "+getWidthDp());
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        Log.i("SCREEN DP","Orientation:"+ newConfig.orientation +" Width = "+getWidthDp());
    }

    public float getWidthDp(){
        DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
        return displayMetrics.widthPixels / displayMetrics.density;
    }
}

Журналы показывают:

I/SCREEN DP: Orientation:1 Width = 411.42856
I/SCREEN DP: Orientation:2 Width = 683.4286
I/SCREEN DP: Orientation:1 Width = 411.42856
I/SCREEN DP: Orientation:2 Width = 683.4286

Не забудьте добавить android:screenOrientation="fullSensor" в манифест, чтобы иметь возможность обрабатывать все ориентации.

0
ответ дан 113408 19 January 2019 в 15:37
поделиться

GridLayoutManager.LayoutParams объясняется в его комментариях:

if the orientation is VERTICAL, the width parameter is ignored and

if the orientation is HORIZONTAL, the height parameter is ignored

because child view is expected to fill all of the space given to it.

суть в том, что

, что LinearLayoutManager всегда будет иметь один столбец; GridLayoutManager «может» иметь 1 (или более) столбцов.

0
ответ дан Martin Zeitler 19 January 2019 в 15:37
поделиться
Другие вопросы по тегам:

Похожие вопросы: